spring-mvc-boot-4 acquisition controller parameters

First, a comment

@RequestParam

@RequestBody

@PathVariable

@DateTimeFormat

@NumberFormat

Second, the acquisition parameters

1, acquisition parameters without comment at

    Note 1. no annotation in the acquisition parameters, the parameter name and the HTTP request requires consistent parameter name. Automatically acquired.

    Note 2. Get an array of parameters without comment at

2, @ RequestParam acquisition parameters

   NOTE: Use @RequestParam Get parameter [parameter before the method to add comments] 

3, get json parameter

  Note: @RequestBody [Parameter Comment] means that it will receive the front end of the body JSON request filed, in the name of the attribute JSON request between the body and the User class is consistent, so that through this layer will Spring MVC mapping relationship JSON User object is converted to the request body.

public Map<String,Object> vaildUser( @RequestBody User user, Errors errors)

Note: @RequestBody use:

 (1) @requestBody annotation used to handle than the default content-type application / content x-www-form-urlcoded encoding, for example: application / json or application / xml like. Which is generally used to process application / json type.
 (2) may request by @requestBody JSON string bound to the body of the bean appropriate, of course, may be bound respectively to the corresponding character string.
 

login(@requestBody String userName,@requestBody String pwd){}

4, passing parameters through the URL
     NOTE 1. @ GetMapping specify a URL, then {. . . } To indicate the location and name of the argument. 
     Note 2. @ PathVariable string configured for the id, which corresponds to the parameter declarations URL. Spring so you know how to get parameters from the URL. Then request http: // localhost: 8080 / user / 1.
     NOTE: @PathVariable Use: Binding placeholders in the URL parameter to the controller of the processing method of ginseng  

     @RequestMapping("/delete/{id}")   
     delete(@PathVariable("id") Long id)  

5, obtaining the formatting parameters  

   @DateTimeFormat (iso = DateTimeFormat.ISO.DATE) Date date, background entity class can convert it into type Date  

   @NumberFormat (pattern = "#, ###. ##") Double number background entity class can convert it into a Double

    In Spring Boot, the date format parameters may not be used @DateTimeFormat, only in the configuration file applic at ion.properties add the following configuration items:

 spring.mvc.date-format=yyyy-MM-dd

 

Guess you like

Origin blog.csdn.net/lidongliangzhicai/article/details/91971578