Several Ways for Spring to Receive web Request Parameters

Keywords: Java JSON curl Spring Lombok

1 Query parameter

Request format: url? Parameter 1 = value 1 & parameter 2 = value 2...
Suitable for both GET and POST
spring can handle query parameters in several ways:

Method 1:
Method parameter name is the request parameter name

  // Query parameter 1
  @RequestMapping(value = "/test/query1", method = RequestMethod.GET)
  public String testQuery1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

 

Method two:
Extracting parameters from HttpServletRequest

  // Query parameter 2
  @RequestMapping(value = "/test/query2", method = RequestMethod.GET)
  public String testQuery2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }


Method three:
The method parameter name can be different from the request parameter name by binding the parameter with the @RequestParam annotation

  // Query parameter 3
  @RequestMapping(value = "/test/query3", method = RequestMethod.GET)
  public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);
    return "username=" + un + ", password=" + pw;
  }


Method four:
Creating an entity class object as a parameter bearer, spring automatically binds parameters to the attributes of the entity class object based on the parameter name

  // Query parameter 4
  @RequestMapping(value = "/test/query4", method = RequestMethod.GET)
  public String testQuery4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

Entity classes are defined as follows:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
  private String username;
  private String password;
}

The third-party library lombok is used here, so you don't need to add get, set and other methods manually in the code, lombok will automatically add.

The curl command to send the request is as follows:

curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'

Interactive messages are as follows:

GET /test/query1?username=aaa&password=bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:01:30 GMT

username=aaa, password=bbb

 

2 Form parameters

The request parameter is not in the url, but in the Body, in the form of: url? Parameter 1 = value 1 & parameter 2 = value 2...
Applicable to POST mode
The form parameter processing method is almost identical to the previous request parameter processing method, except that the method method method is set to a POST method in the RequestMethod annotation.

Method 1:

  // Form parameter 1
  @RequestMapping(value = "/test/form1", method = RequestMethod.POST)
  public String testForm1(String username, String password) {
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

Method two:

  // Form parameter 2
  @RequestMapping(value = "/test/form2", method = RequestMethod.POST)
  public String testForm2(HttpServletRequest request) {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

Method three:

  // Form parameter 3
  @RequestMapping(value = "/test/form3", method = RequestMethod.POST)
  public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
    System.out.println("username=" + un + ", password=" + pw);
    return "username=" + un + ", password=" + pw;
  }

Method four:

  // Form parameter 4
  @RequestMapping(value = "/test/form4", method = RequestMethod.POST)
  public String testForm4(User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

The curl request command is as follows:

curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1

The request and response messages are as follows:

POST /test/form1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Length: 25
Content-Type: application/x-www-form-urlencoded

username=aaa&password=bbb

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:05:35 GMT

username=aaa, password=bbb

 

3 path parameters

The request parameter is a part of the url in the form of: url / parameter 1 / parameter 2..
Suitable for both GET and POST
The code is as follows:

  @RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)
  public String testUrl(@PathVariable String username, @PathVariable String password) {
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

The curl command is requested as follows:

curl -i http://192.168.1.14:8080/test/url/aaa/bbb

The request and response messages are as follows:

GET /test/url/aaa/bbb HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*

HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 26
Date: Thu, 25 Oct 2018 07:07:44 GMT

username=aaa, password=bbb

 

4 json format parameters

The request parameters are in the Body and in JSON format. Request headers need to be added: Content-Type: application/json;charset=UTF-8
Applicable to POST mode
Method 1:
Define entity classes, parse json objects into strength classes, and add RequestBody annotations

  // json Parameter 1
  @RequestMapping(value = "/test/json1", method = RequestMethod.POST)
  public String testJson1(@RequestBody User user) {
    String username = user.getUsername();
    String password = user.getPassword();
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

Method two:
Unlike defining entity classes, you can also parse json requests directly into JSONObject classes

  // json Parameter 2
  @RequestMapping(value = "/test/json2", method = RequestMethod.POST)
  public String testJson2(@RequestBody JSONObject json) {
    String username = json.getString("username");
    String password = json.getString("password");
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

Method three:
You can also parse json objects directly into Map objects

  // json Parameter 3
  @RequestMapping(value = "/test/json3", method = RequestMethod.POST)
  public String testJson3(@RequestBody Map<String, String> userMap) {
    String username = userMap.get("username");
    String password = userMap.get("password");
    System.out.println("username=" + username + ", password=" + password);
    return "username=" + username + ", password=" + password;
  }

The curl command is requested as follows:

curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '
{
    "username" : "aaa",
    "password" : "bbb"
}
' http://192.168.1.14:8080/test/json1

The request and response messages are as follows:

POST /test/json1 HTTP/1.1
Host: 192.168.1.14:8080
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json;charset=UTF-8
Content-Length: 52


{
    "username" : "aaa",
    "password" : "bbb"
}
HTTP/1.1 200 Content-Type: text/plain;charset=UTF-8 Content-Length: 26 Date: Thu, 25 Oct 2018 07:09:06 GMT username=aaa, password=bbb

Posted by Scud on Sat, 19 Jan 2019 06:24:12 -0800