Article directory
- Request processing - use of common parameter annotations
- `PathVariable` gets path variable annotation
- Get request header `@RequestHeader`
- ` @RequestParam ` Get request parameters (referring to the parameters after the question mark, url?a=1&b=2)
- `@CookieValue` to get the cookie value
- @RequestBody Get the data of the request body (POST request)
- Request processing - @RequestAttribute gets the attributes in the request field
- MatrixVariable与UrlPathHelper
- Off topic
Request processing - use of common parameter annotations
annotation:
@PathVariable
path variable@RequestHeader
get request headers@RequestParam
Get request parameters (referring to the parameters after the question mark, url?a=1&b=2)@CookieValue
Get cookie value@RequestAttribute
Get request domain attributes@RequestBody
Get request body [POST]@MatrixVariable
matrix variable@ModelAttribute
This is what happened to us
PathVariable
Get path variable annotation
Take a look at the PathVariable annotation
If the value of a parameter is map, then he will put the kv value in the path into the map
@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
and get it
get request headers@RequestHeader
got it
@RequestParam
Get 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
@CookieValue
Get cookie value
The comment in @CookieValue
probably means that we can also declare a cookie type so that we can take all the cookies.
Some browsers don't have cookies. Don't worry about it here.
@RequestBody Get the data of the request body (POST request)
got it
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
MatrixVariable与UrlPathHelper
-
Syntax: Request path:
/cars/sell;low=34;brand=byd,audi,yd
This kind of band; is the matrix variable -
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.
-
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
@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
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.