SpringBoot get http request parameter method

SpringBoot get http request parameter method

Original: https://www.cnblogs.com/zhanglijun/p/9403483.html

There are seven Java front end came backstage acquisition method parameters, a little list of what.

Form 1 directly inside the parameters written to the corresponding formal parameters of the method to the Controller, this method is suitable for acquiring parameters get filed, not suitable for post submission.

    /**
     * 1.直接把表单的参数写在Controller相应的方法的形参中
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(String username,String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

url form: http:? // localhost / jayvee / demo / addUser username = wjw & password = 123456 consistent parameter names submitted by the parameter name must be defined and Controller methods.

2. Use HttpServletRequest to obtain parameters for post and get methods.

    /**
     * 2、通过HttpServletRequest接收
     * @param request
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(HttpServletRequest request) {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

3. to get the parameters through the establishment of a bean, suitable for post and get.
First, create a form and corresponding bean

package demo.model;
public class UserModel {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

Bean package used to create the front end of the received parameters transmitted

    /**
     * 3、通过一个bean来接收
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(UserModel user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "demo/index";
    }

4. Get the parameters passed into PathVariable.

    /**
     * 4、通过@PathVariable获取路径中的参数
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/addUser/{username}/{password}",method=RequestMethod.GET)
public String addUser(@PathVariable String username,@PathVariable String password) {
        System.out.println("username is:"+username);
        System.out.println("password is:"+password);
        return "demo/index";
    }

For example, access http: // localhost / jayvee / demo / addUser4 / wjw / 123465, then automatically the URL template variable {username} and {password} to bind to the same name annotation @PathVariable parameters, i.e. after the parameters username = wjw, password = 123465.

5. POST request acquiring annotation @ModelAttribute FORM form data
jsp Form

<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post"> 
     用户名:&nbsp;<input type="text" name="username"/><br/>
     密&nbsp;&nbsp;码:&nbsp;<input type="password" name="password"/><br/>
     <input type="submit" value="提交"/> 
     <input type="reset" value="重置"/> 
</form>

Java Controller

    /**
     * 5、使用@ModelAttribute注解获取POST请求的FORM表单数据
     * @param user
     * @return
     */
    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public String addUser(@ModelAttribute("user") UserModel user) {
        System.out.println("username is:"+user.getUsername());
        System.out.println("password is:"+user.getPassword());
        return "demo/index";
    }

6. @RequestParam annotation request parameters bound to the reference method
when an exception occurs when there is a request parameter username does not exist, by setting properties required = false solution, for example: @RequestParam (value = "username" , required = false)

/**
 * 6、用注解@RequestParam绑定请求参数到方法入参
 * @param username
 * @param password
 * @return
 */
@RequestMapping(value="/addUser",method=RequestMethod.GET)
public String addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
    System.out.println("username is:"+username);
    System.out.println("password is:"+password);
    return "demo/index";
}

7. Request @RequestBody annotation parameters into parameters for binding to the POST request method

/**
     * 7、用注解@Requestbody绑定请求参数到方法入参
      * @param username
     * @param password
     * @return
     */
    @RequestMapping(value="/addUser",method=RequestMethod.POST)
    public String addUser(@RequestBody UserDTO userDTO) {
        System.out.println("username is:"+userDTO.getUserName());
        System.out.println("password is:"+userDTO.getPassWord());
        return "demo/index";
    }

//UserDTO 这个类为一个实体类,里面定义的属性与URL传过来的属性名一一对应。

Above acquisition method parameter is passed into the front seven summarize the background springbooot.

Difference @RequestParam and @RequestBody of the spring boot

1, description of the problem
since the front and rear ends are separated from the project, and therefore the background using spring boot, made of micro-services, expose only the interface. Interface design is restful style, in the get request, receive parameters of background notes will complain RequestBody; in the post request, notes backstage reception parameters is also being given RequestParam.

2, the cause of the problem

Since the spring of RequestParam annotations received parameter from requestHeader, i.e. request header, i.e. in the url format xxx? Username = 123 & password = 456, and RequestBody annotated received parameter is from the requestBody, i.e. request body in.

3, the solution

In summary therefore, if the request to get background notes should receive parameters RequestParam, if the request for the post, the background notes is to receive parameters RequestBody. Attach two examples, the following theme:

4, get request
Here Insert Picture Description
5, post requests
Here Insert Picture Description
In addition, there is an application scenario, the interface specification is resultful style, for example: If you want to get the number of queries answer questions under this provision an id, then the background you need to get a dynamic parameter , which is annotated as @PathVariable, and the value should be requestMapping value = "/ {id} / queryNum", screenshot as follows:
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/wjw1014/p/11611312.html