1. Get request parameters
1.1 Get query parameters
In GET requests, we usually pass data by querying parameters. Can be used@RequestParam
Annotation to receive these parameters.
import ; import ; import ; @RestController public class UserController { @GetMapping("/users") public String getUsers(@RequestParam String name, @RequestParam int age) { return "User name: " + name + ", Age: " + age; } }
1.2 Get path parameters
For parameters that need to be passed in the URL, you can use@PathVariable
Annotation.
@GetMapping("/users/{id}") public String getUserById(@PathVariable Long id) { return "User ID: " + id; }
2. Process form submission
2.1 Processing form data
When processing form data submitted by a POST request, you can use@ModelAttribute
Annotation binds form data to an object.
import ; import ; import ; @RestController public class UserController { @PostMapping("/users") public String createUser(@ModelAttribute User user) { // Logic to save user information to the database return "User created: " + user; } }
CorrespondingUser
kind:
public class User { private String name; private String email; // Getters and Setters }
3. Process JSON data
3.1 Receive JSON data
For data submitted in JSON format, you can use@RequestBody
Annotation binds it to an object.
import ; import ; import ; @RestController public class UserController { @PostMapping("/users/json") public String createUser(@RequestBody User user) { // Logic to save user information to the database return "User created: " + user; } }
4. Return JSON data
The Spring Boot controller can easily return JSON data, just return an object, and Spring Boot will automatically convert it to JSON format.
import ; import ; @RestController public class UserController { @GetMapping("/users/json") public User getUserJson() { User user = new User(); ("John Doe"); ("john@"); return user; } }
5. Process file upload
5.1 Single file upload
Can be used@RequestParam
Annotation receives uploaded files.
import ; import ; import ; import ; @RestController public class FileController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { if (()) { return "File is empty"; } // Logic of saving files return "File uploaded successfully: " + (); } }
5.2 Multi-file upload
Supporting multiple file uploads is also very simple, just put@RequestParam
The parameter type is set toMultipartFile[]
。
@PostMapping("/upload/multiple") public String uploadMultipleFiles(@RequestParam("files") MultipartFile[] files) { for (MultipartFile file : files) { if (()) { return "One or more files are empty"; } // Logic of saving files } return "Files uploaded successfully"; }
6. Summary
Through this article, you have mastered a variety of ways to process user data in Spring Boot controllers, including obtaining request parameters, processing form submissions, receiving and returning JSON data, and processing file uploads. These skills are the basis for building RESTful APIs and web applications. In actual development, flexibly using these technologies can meet various business needs and provide efficient and flexible interface services. I hope this article can help you become more skillful in Spring Boot development.
The above is a detailed explanation of how to process user data in SpringBoot controller. For more information about SpringBoot controller processing user data, please follow my other related articles!