preamble
In modern Web development, the architecture of front-end and back-end separation has become mainstream. The front-end is responsible for displaying the page and user interaction, while the back-end is responsible for handling business logic and data storage. In this architecture, the front-end needs to send the user input data to the back-end for processing. Spring Boot, as a rapid development framework, provides a variety of ways to receive front-end data.
This article will introduce Spring Boot to receive front-end data in several common ways.
reception method
@RequestParam
This is the most basic one, which maps to the method's parameters by requesting parameter names, e.g.
import ; import ; import ; @RestController public class MyController { @GetMapping("/greeting") public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) { return ("Hello %s!", name); } }
In this example, an HTTP GET request named "greeting" maps to "/greeting". This mapping method accepts a request parameter named "name". If the "name" parameter is not provided in the request, then the default value of "World" will be used.
For example, if you visit "http://localhost:8080/greeting?name=John", you will get "Hello John!". If you visit only "http://localhost:8080/greeting" (without providing the "name" parameter), you will get "Hello World!".
@RequestHeader
In Spring Boot, we can pass parameters in the HTTP request header through the @RequestHeader annotation. This annotation can be applied to the method parameters of the controller and Spring will automatically bind the corresponding parameter values in the request header to the method parameters.
import ; import ; import ; @RestController public class MyController { @GetMapping("/test") public String test(@RequestHeader("User-Agent") String userAgent) { return "User-Agent: " + userAgent; } }
In this example, we have defined a file namedtest
which receives a GET request handler method nameduserAgent
parameter of the method. This is accomplished by prefixing the method argument with the@RequestHeader("User-Agent")
annotation, we tell Spring Boot to take the value of the parameter named "User-Agent" from the HTTP request header and bind it to theuserAgent
Parameters on.
When we send a GET request with the "User-Agent" header to the "/test" path, Spring Boot passes the "User-Agent" value in the request header to thetest
method and returns "User-Agent:" plus a string of that value.
@CookieValue
In Spring Boot, we can use the @CookieValue annotation to get the value of a cookie. This annotation can be used on a method parameter to indicate the value to be retrieved from the cookie.
Here is a simple example:
import ; import ; import ; @RestController public class CookieController { @GetMapping("/getCookie") public String getCookie(@CookieValue("cookieName") String cookieValue) { return "The value of the cookie is: " + cookieValue; } }
In this example, we have created a controller named CookieController. In this controller, we have a method named getCookie that takes a parameter named cookieValue. The value of this parameter is retrieved from the cookie named "cookieName".
When you set a cookie named "cookieName" in your browser and access the /getCookie path, you will see the message "The value of the cookie is: [cookie value]" returned.
Note: In order for the @CookieValue annotation to work, you need to have Spring's web support enabled in your Spring Boot application. You can enable it by adding the @EnableWebMvc or @EnableWebFlux annotations on your main configuration class.
@PathVariable
In Spring Boot, we can use the @PathVariable annotation to get parameters from URL paths. This annotation is usually used in controller methods of RESTful API.
Here is a simple example:
import ; import ; import ; @RestController public class MyController { @GetMapping("/greeting/{name}") public String greeting(@PathVariable("name") String name) { return "Hello, " + name; } }
In this example, we define a GET request named "greeting" that maps to the URL "/greeting/{name}".The @PathVariable("name") annotation tells Spring Boot to take the parameter named " name" parameter from the URL path and pass its value to the method greeting.
For example, if you access "http://localhost:8080/greeting/John", then this method will return "Hello, John".
@RequestBody
Passing parameters via @RequestBody is a common way in Spring Boot, which is mainly used to handle JSON data in POST requests. This way you can bind the JSON data in the request body to the method parameters.
Here is a simple example:
First, we need to create a POJO (Plain Old Java Object) to represent our data model. For example, we can create a User class:
public class User { private String name; private int age; // getters and setters }
We can then use the @RequestBody annotation in the Controller to receive the JSON data in the request body:
import ; import ; import ; @RestController public class UserController { @PostMapping("/user") public User createUser(@RequestBody User user) { // Here, the user object is the JSON data in the request body // We can handle this object as needed return user; } }
In this example, when we send a POST request to the /user path and include a JSON object in the body of the request, Spring Boot automatically converts this JSON object to a User object and passes it to the createUser method.
HttpServletRequest
In Spring Boot, you can get the request parameters through the HttpServletRequest object.The HttpServletRequest object provides some methods such as getParameter(), getHeader() and so on, which can be used to get the request parameters.
Here is a simple example:
import ; import ; import ; @RestController public class MyController { @GetMapping("/hello") public String hello(HttpServletRequest request) { String name = ("name"); return "Hello, " + name; } }
In this example, we have created a controller class named MyController and defined a method named hello. This method receives an HttpServletRequest object as a parameter.
When we visit the /hello path, Spring Boot automatically passes the current HttpServletRequest object to the hello method. We can then use the getParameter("name") method to get the value of the request parameter "name".
For example, if you access /hello?name=John, the result will be "Hello, John".
The above is Spring Boot commonly used in several ways to receive front-end parameters, you can choose to use according to need.
summarize
to this article on SpringBoot to receive front-end parameters of several commonly used ways to this article, more related to SpringBoot to receive front-end parameters, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!