Common parameter annotations for springboot request processing springboot issue 7

Request processing - use of common parameter annotations

annotation:

  • @PathVariablepath variable
  • @RequestHeaderget request headers
  • @RequestParamGet request parameters (referring to the parameters after the question mark, url?a=1&b=2)
  • @CookieValueGet cookie value
  • @RequestAttributeGet request domain attributes
  • @RequestBodyGet request body [POST]
  • @MatrixVariablematrix variable
  • @ModelAttribute

This is what happened to us
insert image description here

PathVariableGet path variable annotation

Take a look at the PathVariable annotation

insert image description here

If the value of a parameter is map, then he will put the kv value in the path into the map
insert image description here

insert image description here

@RestController
public class ParameterTestController {
    
    

    @GetMapping("/car/{id}/owner/{name}")
    public Map<String,Object> getCar(@PathVariable("id")Integer id ,
                                     @PathVariable("name") String name,
                                     @PathVariable Map<String,String> pv) {
    
    

    Map<String,Object> map = new HashMap<>();
    map.put("id",id);
    map.put("name",name);
    map.put("pv",pv);

        return map;
    }


}

This map we try to get all the values
insert image description here
​​and get it
insert image description here

get request headers@RequestHeader

insert image description here
insert image description here
got it

insert image description here

@RequestParamGet request parameters (referring to the parameters after the question mark, url?a=1&b=2)

The map is the same as the above, which encapsulates all
insert image description here
insert image description here

insert image description here

@CookieValueGet cookie value

The comment in @CookieValue
probably means that we can also declare a cookie type so that we can take all the cookies.
insert image description here
insert image description here
insert image description here
Some browsers don't have cookies. Don't worry about it here.

@RequestBody Get the data of the request body (POST request)

insert image description here
insert image description here
insert image description here
got it
insert image description here

Request processing - @RequestAttribute gets the attributes in the request field

 @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
    
    

        request.setAttribute("msg","成功了...");
        request.setAttribute("code",200);
        return "forward:/success";  //转发到  /success请求
    }

    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,
                       @RequestAttribute(value = "code",required = false)Integer code,
                       HttpServletRequest request){
    
    
        Object msg1 = request.getAttribute("msg");

        Map<String,Object> map = new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");

        map.put("reqMethod_msg",msg1);
        map.put("annotation_msg",msg);
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);

        return map;
    }

The value of this false is that there is no msg item
insert image description here

MatrixVariable与UrlPathHelper

  1. Syntax: Request path: /cars/sell;low=34;brand=byd,audi,ydThis kind of band; is the matrix variable

  2. SpringBoot disables the function of matrix variables by default

    • Manual opening: principle. Handling of paths. UrlPathHelper's removeSemicolonContent is set to false, allowing it to support matrix variables.
  3. Matrix variables must have url path variables to be parsed

Why is there such a thing as a matrix variable?

Page development, cookie is disabled, how to use the content in the session?

We save things in the session
session.set(a,b)—>
jsessionid—>
what we save is in the cookie
cookie ----> Carry it every time we send a request.

After the server receives the cookie, it will find the session object according to the jsessionid, and then you can use the get method to find what you have saved.
But if the cookie is not allowed to use the things stored in our session, it will not be found.
In order to solve this problem, url rewriting occurs. Let's write the value of the cookie into the url.
In order to distinguish it from ordinary variables, we use ; to
rewrite the interval url: /abc;jsesssionid=xxxx to pass the value of the cookie using a matrix variable.
For example, /boss/1;age= 20/2;age=20
1;age=20 Before the
semicolon is the access path and after the semicolon is the matrix variable

test

insert image description here

      @GetMapping("/cars2/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
    
    
        Map<String,Object> map = new HashMap<>();

        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }

open matrix variable

package com.example.springboot_study_02.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;

import java.util.HashMap;
import java.util.Map;

@Configuration(proxyBeanMethods = false)
public class WebConfig {
    
    
    //implements WebMvcConfigurer
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
    
    
        return new WebMvcConfigurer() {
    
    
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
    
    
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                // 不移除;后面的内容。矩阵变量功能就可以生效
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }

//    @Override
//    public void configurePathMatch(PathMatchConfigurer configurer) {
    
    
//
//        UrlPathHelper urlPathHelper = new UrlPathHelper();
//        // 不移除;后面的内容。矩阵变量功能就可以生效
//        urlPathHelper.setRemoveSemicolonContent(false);
//        configurer.setUrlPathHelper(urlPathHelper);
//    }
}

Remember to turn on the blocker

spring:
  resources:
    static-locations: [classpath:/gb/,classpath:/gb1/]
  mvc:
    hiddenmethod:
      filter:
        enabled: true

insert image description here

Off topic

Today is March 25, 2022.
My first day note was written on March 20, which means that I only watched 30 episodes of online courses in five days. It can be said that my learning efficiency is relatively unsuccessful. Well, it 's
mainly the school classes. I really don't want to go to the junior year. I still rely on a lot of school stuff every day. I'm afraid that if I don't graduate, I'll starve to death
. I feel that I have gained a lot from the design mode
. The things written by Xiaoying are far better than mine, but my first article has just started two days. It is higher than Xiaoying in the rankings, which is unacceptable.

Guess you like

Origin blog.csdn.net/qq_47431361/article/details/123737233