Detailed explanation of @PathVariable, @CookieValue, @RequestHeader

1. @PathVariable binds the uri template variable value

Placeholder parameters can be bound to method parameters through @PathVariable, for example:

@RequestMapping(value="/info/{uid}", method=RequestMethod.GET)
public User getUserById(@PathVariable Long uid) {
    
    
	return userService.getUserById(uid);
}

If the request is "url/info/16", spring will automatically bind 16 to the variable uid of the same name annotated by @PathVariable

2. @CookieValue binds Cookie data value

public String test(@CookieValue(value="SESSIONID", defaultValue="") String sessionId) {
    
    
	return sessionId;
}

Automatically bind the value of SESSIONID to the string sessionId, if there is no sessionId in the cookie, it will be empty by default

The incoming data type can also be javax.servlet.http.Cookie type

public String test2(@CookieValue(value="SESSIONID", defaultValue="") Cookie sessionId){
    
    

}

3. @RequestHeader binds request header data

@RequestMapping(value="/header")  
public String test(  
       @RequestHeader("User-Agent") String userAgent,  
       @RequestHeader(value="Accept") String[] accepts){
    
    

}

The above configuration will automatically bind the "User-Agent" value in the request header to userAgent, and bind the value of "Accept" to accepts

Guess you like

Origin blog.csdn.net/weixin_44860226/article/details/129632030