Home Java javaTutorial Java 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
java spring Request 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;
  }
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

The entity class is defined as follows:

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

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'
Copy after login

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
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

The curl request command is as follows:

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

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
Copy after login

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;
  }
Copy after login

The request curl command is as follows:

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

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
Copy after login

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;
  }
Copy after login

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;
  }
Copy after login

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, String> userMap) {
    String username = userMap.get("username");
    String password = userMap.get("password");
    System.out.println("username=" + username + ", password=" + password);    
    return "username=" + username + ", password=" + password;
  }
Copy after login

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
Copy after login

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
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles