post parameter passing mode and method of reception parameters

1. url in the address bar of mass participation

To? URL split and transmit data to & connected between parameters,

如:localhost:8080/user/?id=2&userName="王慢慢"&password="123655"

send

receive

    /**
     *
     *增加与修改的区别就是id是否为空,id为空是增加,id不为空是修改
     * @param user
     * @return
     */
    @PostMapping("/user")
    public User post( Long id,
                     @RequestParam String userName,
                     @RequestParam String password ){
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setPassword(password);
        return userService.save(user);
    }

Or accept a User class object as a parameter

  /**
     *
     *增加与修改的区别就是id是否为空,id为空是增加,id不为空是修改
     * @param user
     * @return
     */
    @PostMapping("/user")  
  public User post(User user){

        return userService.save(user);
    }

2. application/x-www-form-urlencoded

send

This should be the most common way to POST submit the data. Native browser <form \> form, if you do not set the enctype attribute, then the default will submit data to application / x-www-form- urlencoded manner. Postman is in the corresponding x-www-form-urlencoded post under way.
 Network interface request testing tools : ,

The effects of the same

                       

A domestic software

In jQuery, Ajax request, the default value of the Content-Type is application / x-www-form-urlencoded; charset = utf-8

receive

    /**
     *
     *增加与修改的区别就是id是否为空,id为空是增加,id不为空是修改
     * @param user
     * @return
     */
    @PostMapping("/user")
    public User post( Long id,
                     @RequestParam String userName,
                     @RequestParam String password ){
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setPassword(password);
        return userService.save(user);
    }

Or accept a User class object as a parameter

  /**
     *
     *增加与修改的区别就是id是否为空,id为空是增加,id不为空是修改
     * @param user
     * @return
     */
    @PostMapping("/user")  
  public User post(User user){

        return userService.save(user);
    }

2. application/json

send

In the postman, you can select the raw body options, and then select the right JSON

receive

Because the class object is received, it is necessary to use a reference when receiving the parcel @RequestBody received

  /**
   *增加与修改的区别就是id是否为空,id为空是增加,id不为空是修改
   * @param user
   * @return
   */
  @PostMapping("/user")
    public User post(@RequestBody User user){
        return userService.save(user);
    }

 

Published 153 original articles · won praise 6 · views 2339

Guess you like

Origin blog.csdn.net/yangshengwei230612/article/details/103887721