@requestParam @requestBody @pathVariable

1. @ PathVariable

When using the @requestMapping URI template style mapping, that is, someUrl/{paramId}, paramId can bind its passed value to the method’s parameters via the @pathVariable annotation.

Sample code:

@Controller
@RequestMapping("/owners/{ownerId}")
public class test {
 
  @RequestMapping("/pets/{petId}")
  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {    
    // implementation omitted}}Copy the code

The above code binds the values of the ownerId and petId variables in the URI template to the method parameters. If the method parameter name is inconsistent with the variable name in the URI template to be bound, specify the name in the URI template in @pathVariable (” name “).

2. Correct use of @requestBody

1. The @requestBody annotation is often used to handle content-Type content that is not the default Application/X-www-form-urlcoded, such as Application/JSON or Application/XML. It is typically used to handle application/ JSON types. The @requestBody accepts a JSON string, which must be a string.

You can use @requestbody to bind the JSON string in the requestBody to the corresponding bean or, of course, to the corresponding string separately.

Such as:

$.ajax({
       url:"/login".type:"POST".data:'{"userName":"admin","pwd","admin123"}',
       content-type:"application/json charset=utf-8".success:function(data){
       		alert("request success ! "); }});Copy the code
@requestMapping("/login")
public void login(@requestBody String userName,@requestBody String pwd){System. Out. Println (userName +":"+pwd);
}
Copy the code

In this case, I assign the values of two variables in a JSON string to two strings, but suppose I have a User class that has the following fields:

  • String userName;
  • String pwd;

The above parameter can be changed to the following form: @requestBody User User this form will assign the value from the JSON string to the corresponding attribute in the User. Note that the key in the JSON string must correspond to the attribute name in the user; otherwise, the request will not pass.

@requestMapping("/login")
public void login(@requestBody User user){System. Out. Println (user. GetUserName () +":"+user.getPwd());
}
Copy the code

3, @ RequestParam

RequestParam can accept properties of simple types as well as object types.

RequestParam has three configuration parameters:

  • Required indicates whether it is required. The default is true, yes.
  • DefaultValue Sets the default values for the request parameters.
  • Value is the parameter name (equivalent to the key value) of the received URL.
package com.day01springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("hello")
public class HelloController {
 
    / * * * to receive general request parameters * http://localhost:8080/hello/show16? Name = LinuxSIR * The name in the URL parameter must be and@RequestParam* (" name ")@return* /
    @RequestMapping("show16")
    public ModelAndView test16(@RequestParam("name")String name){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello2");
        mv.addObject("msg"."Receive normal request parameters:" + name);
        return mv;
    }
 
    / * * * to receive regular request parameters * http://localhost:8080/hello/show17 * no name in the url won't complain, have displayed as *@return* /
    @RequestMapping("show17")
    public ModelAndView test17(@RequestParam(value="name",required=false)String name){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello2");
        mv.addObject("msg"."Receive normal request parameters:" + name);
        return mv;
    }
 
    / * * * to receive general request parameters * http://localhost:8080/hello/show18? Display name = 998 to 998 * http://localhost:8080/hello/show18? Name is displayed as Hello *@return* /
    @RequestMapping("show18")
    public ModelAndView test18(@RequestParam(value="name",required=true,defaultValue="hello")String name){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello2");
        mv.addObject("msg"."Receive normal request parameters:" + name);
        returnmv; }}/* The command output is as follows: show16------ For receiving common requests: Linuxsir show17------ for receiving common requests :null show18------ for receiving common requests :hello */    
Copy the code

4, @ Param

@param is a annotation in Mybatis. When using annotations to simplify XML configuration, the function of @param annotation is to name parameters. After naming parameters, the parameter value can be obtained according to the name, and the parameter can be correctly passed into SQL statement. Take a look at the following example:

public interface Mapper { 
   
@Select("select s_id id,s_name name,class_id classid from student where s_name= #{aaaa} and class_id = #{bbbb}") 
public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id);  
   
@Delete.@Insert. }Copy the code