# SpringBoot back-end method to obtain interface parameters

SpringBoot back-end method to obtain interface parameters

Back-end interface to obtain parameters

@RequestMapping annotation
  • The class-level and method-level annotations indicate the path of front-end and back-end resolution. There is a value attribute (default for a parameter) to specify url path resolution, and a method attribute to specify the submission method (the default is get submission)

@RequestBody annotation acquisition (from Body)
  • Note: The Content-Type in the request header must be application/json , otherwise it is not supported. The parameters received by the backend can only be the JSON format parameters passed in the body, and the spliced ​​parameters in the Url cannot be obtained.

Insert picture description here

  • Correct example
 /**
   * 从body中获取参数
   * @param map
   * @return
   */
@PostMapping("/test1")
public Object testOne(@RequestBody Map<String,String> map){
    
    
    return JSONObject.toJSONString(map);
}

@RequestMapping("/test2")
public Object testTwo(@RequestBody Map<String,String> map){
    
    
    return JSONObject.toJSONString(map);
}

@PathVariable (from Url)
  • @PathVariable : url parameter annotation, generally used to get parameters from url

  • Note: The parameters are read from the URL in the form of **/parameter1/parameter2/ , so both get and post requests are supported, and the Content-Type** in the request header does not matter.
    Insert picture description here
    Insert picture description here

  • Correct example

@PostMapping("/test3/{name}/{age}")
public Object testThree(@PathVariable(value = "name") String xingming,@PathVariable(value = "age") String nianling){
    
    
    return xingming+":"+nianling;
}

@GetMapping("/test4/{name}/{age}")
public Object testFour(@PathVariable(value = "name") String xingming,@PathVariable(value = "age") String nianling){
    
    
    return xingming+":"+nianling;
}

@RequestParam (splicing parameters in Url)
  • @RequestParam : Request parameter rule annotation. The value attribute matches the parameter passed by the front desk (default for a parameter), the required attribute, whether this field must be passed a value (boolean, the default is true), defaultValue is the default value of this parameter (when this parameter exists, the front desk does not need to pass the parameter, required Is false).

  • Note: The request url is sent in the form of ? parameter 1=content & parameter 2=content .
    Insert picture description here

  • Correct example

@RequestMapping("/test5")
public Object testTwo(@RequestParam(value = "name") String name,@RequestParam(value = "age") String age){
    
    
    return name+":"+age;
}

Guess you like

Origin blog.csdn.net/qq_37248504/article/details/113274412