search
HomeJavajavaTutorialJava explains in detail how Spring receives web request parameters

Java explains in detail how Spring receives web request parameters

Oct 25, 2018 pm 04:39 PM
javaspringRequest parameters

This article brings you a detailed explanation of how Spring receives web request parameters in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1 Query parameters

Request format: url?Parameter 1=value 1&Parameter 2=value 2...
Applies to both For GET and POST methods
There are several ways to write query parameters in spring:

Method one:
The method parameter name is the request parameter name

  // 查询参数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:
Extract parameters from HttpServletRequest

  // 查询参数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 and the request parameter name can be different, and the parameters are bound through the @RequestParam annotation

  // 查询参数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:
Create an entity class object as the parameter carrier. Spring will automatically bind the parameters to the properties of the entity class object based on the parameter name.

  // 查询参数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;
  }

The entity class is defined as follows:

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

Used here With the third-party library lombok, there is no need to manually add get, set and other methods in the code, lombok will be added automatically.

The curl command to send the request is as follows:

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

The interactive message is 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 parameters are not in the url, but in the Body. The format is: url? Parameter 1=value 1¶meter 2=value 2...

Applicable to POST method

The form parameter processing method is almost exactly the same as the previous request parameter processing method, except that the method method is set to the POST method in the RequestMethod annotation

Method one:

  // 表单参数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:

  // 表单参数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:

  // 表单参数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:

  // 表单参数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 message is 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=bbbHTTP/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 parameter

The request parameter is part of the url, the format is: url/parameter 1/parameter 2...
Applicable to both GET and POST methods
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 request curl command is as follows:

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

The request and response message 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 are in json format. Need to add request header: Content-Type: application/json;charset=UTF-8
Applicable to POST method
Method 1:
Define entity class, parse json object into strength class, need to add RequestBody annotation

  // json参数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 2:

If you don’t want to define an entity class, you can also parse the json request directly into the JSONObject class

  // json参数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 3:

Also The json object can be parsed directly into a Map object

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

The request curl command is 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 message is 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

The above is the detailed content of Java explains in detail how Spring receives web request parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools