The difference between @RequestParam, @PathParam, @PathVariable and other annotations in Restful

@RequestParamand @PathVariableannotations are used to receive requests from requests. Both can receive parameters. The key difference is @RequestParamthat the value @PathVariableis filled from a URI template.

@RequestParam

Look at the following piece of code:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

According to the above URL, you can get it in this way

public String getDetails(
    @RequestParam(value="param1", required=true) String param1,
        @RequestParam(value="param2", required=false) String param2){
...
}

@RequestParamThe following four parameters are supported

  • defaultValue If this request does not carry this parameter, or the parameter is empty, then the default value will be enabled
  • name The name of the parameter to be bound to this time, it should be the same as the one above the URL
  • Is this parameter required?
  • value has the same function as name, and is an alias of the name attribute

PathVariable

This annotation can identify a template in the URL, let's see a URL below

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

For the above url you can write:

@RequestMapping("/hello/{id}")
    public String getDetails(@PathVariable(value="id") String id,
    @RequestParam(value="param1", required=true) String param1,
    @RequestParam(value="param2", required=false) String param2){
.......
}

The difference is obvious

@PathParam

This annotation is the pathVariablesame as that of spring, and it is also template-based, but this is an implementation under the jboss package, and the above is an implementation of spring, and the package must be imported.

@QueryParam

@QueryParamIt is originally provided by JAX-RS, which is consistent with Spring's RequestParam

@ResponseBody

responseBody indicates the way the server returns when it returns, returns the content or object as the HTTP response body, there are many values, generally set to json

@RequestBody

This request is generally used when a post request is made, and the parameters are thrown in the requestbody.

Transfer: https://blog.csdn.net/u011410529/article/details/66974974

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727653&siteId=291194637