1. Preface
Hello everyone, in SpringMVC development, parameter transfer is a core issue that developers have to face every day. Different business scenarios require different parameter reception methods, and reasonable parameter processing can greatly improve development efficiency and code quality. This article will be completed through a completeRequestController
Case study, explain 10 common parameter transfer methods in detail to help you thoroughly master SpringMVC parameter transfer skills.
2. Text
Official Documentation: Spring Framework Documentation:: Spring Framework
/spring-framework/reference/
2.1 Basic parameter transfer
2.1.1 Single parameter
@RequestMapping("/r1") public String r1(String keyword){ return "Receive parameters:" + keyword; }
- Call Example:
/request/r1?keyword=spring
- Features:
- The parameter name is strictly matched with the method parameter name
- Automatically complete basic type conversion (String→int, etc.)
- If the parameter does not exist, it will be injected
null
2.1.2 Multiple parameters
@RequestMapping("r2") public String r2(String name, int id){ return "receive:" + name + id; }
- Call Example:
/request/r2?name=Alice&id=1001
- Things to note:
- The basic type parameter cannot be null (such as int)
- Recommended packaging type
Integer
Avoid null pointer exceptions
Avoid null pointers:
@RequestMapping("r3") public String r3(Integer number){ return "receive" + number; }
2.2 Object parameter binding
2.2.1 Automatically encapsulate objects
@RequestMapping("r4") public String r4(student s1){ return "receive" + (); }
- Call Example:
/request/r4?id=1002&name=Bob
- Implementation mechanism:
- Spring automatically creates Student instances
- Property injection through setter method
- Support nested object attribute binding
2.2.2 Parameter alias processing
@RequestMapping("/r5") public String r5(@RequestParam(value = "n", required = false)String name){ return "receive" + name; }
- Notes:
value
: Specify parameter aliasrequired
: Whether the parameter is required (default true)defaultValue
: Default value settings
2.3 Collection type processing
2.3.1 Array Receive
@RequestMapping("/r6") public String r6(String[] array){ return "receive" + (array); }
- Call method:
/request/r6?array=1&array=2
/request/r6?array=1,2,3
2.3.2List collection reception
@RequestMapping("/r7") public String r7(@RequestParam List<Integer> list){ return "receive:" + list; }
- Pay special attention:
- Must use
@RequestParam
annotation- Prohibit the use of specific implementation classes such as ArrayList
- It is recommended to use packaging types to avoid transformation exceptions
2.4JSON parameter processing
2.4.1 Introduction to JSON
JSON (JavaScript Object Notation, JavaScript object notation) is a lightweight data exchange format that is easy to read and write by people, and is also easy to machine parse and generate. It is based on plain text and is encoded using Unicode, with extensive compatibility and flexibility, and is often used for data transmission between network applications.
The JSON format organizes data in the form of key-value pairs, supporting two main data structures:
- Object
- An unordered set of key-value pairs enclosed in braces "{}".
- The key must be of string type, and the value can be a string, a numeric value, a boolean value, an array, an object or
null
。- Keys and values are separated by English colon ":", and key-value pairs are separated by English comma ",".
- For example:
{ "name": "Kimi", "age": 25, "isStudent": false, "hobbies": ["reading", "traveling"], "address": { "city": "Beijing", "country": "China" } }
- Array
- An ordered set of values enclosed in square brackets "[]".
- The values in an array can be of any type, including strings, numerics, booleans, objects, arrays, or
null
。- The values in the array are separated by an English comma ",".
- For example:
[ "apple", 123, true, null, {"key": "value"}, [1, 2, 3] ]
In SpringMVC, the automatic conversion of JSON parameters depends onHttpMessageConvertersystem. When the requestedContent-Type
forapplication/json
When the framework is usedMappingJackson2HttpMessageConverter
Perform data binding.
- Processing process:
- Front-end sends JSON format request body
- DispatcherServlet selects matching message converter
- Jackson library deserializes JSON into Java object
- pass
@RequestBody
Injection method parameters
2.4.2 Passing JSON parameters
@RequestMapping("/r8") public String r8(@RequestBody student student1){ return (); }
- Request request:
- Content-Type must be
application/json
- Need Jackson dependency support
- Support nested complex object parsing
- Request Example:
{"id": 1003, "name": "Carol"}
2.5RESTful style parameters
@RequestMapping("/path/{pathid}") public String r9(@PathVariable Integer pathid){ return "Get path id" + pathid; }
- Call Example:
/request/path/1004
- Best Practices:
- Suitable for resource positioning scenarios
- Support regular expression verification
- Can cooperate
@GetMapping
Use of comments
2.6 File upload processing
@RequestMapping("/r10") public String r10(MultipartFile file) throws IOException { (new File("D:\\Desktop\\" + ())); return "File upload successfully"; }
- Form needs to be set
enctype="multipart/form-data"
- Supports multiple files upload simultaneously
- Pay attention to file storage path permission issues
2.7 Complete debugging code
package ; import ; import .*; import ; import ; import ; import ; class student{ int id; String name; public student() {} public student(int id, String name) { = id; = name; } public int getId() { return id; } public void setId(int id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } @Override public String toString() { return (); } } @RequestMapping("/request") @Controller @ResponseBody public class RequestController { @RequestMapping("/r1") public String r1(String keyword){ return "Receive parameters:" + keyword; } @RequestMapping("r2") public String r2(String name, int id){ return "receive:" + name + id; } @RequestMapping("r3") public String r3(Integer number){ return "receive" + number; } @RequestMapping("r4") public String r4(student s1){ return "receive" + (); } //Receive parameter n from the front end and assign it to name @RequestMapping("/r5") public String r5(@RequestParam(value = "n", required = false)String name){ return "receive" + name; } //Pass an array @RequestMapping("/r6") public String r6(String[] array){ return "receive" + (); } //Pass the collection @RequestMapping("/r7") public String r7(@RequestParam List<Integer> list){ return "receive:" + list; } // Pass json @RequestMapping("/r8") public String r8(@RequestBody student student1){ return (); } //Get parameters from the URL @RequestMapping("/path/{pathid}") public String r9(@PathVariable Integer pathid){ return "Get path id" + pathid; } //Upload file @RequestMapping("/r10") public String r10(MultipartFile file) throws IOException { (()); (new File("D:\\Desktop\\" + ())); return "File upload successfully"; } }
3. Summary
Today’s sharing ends here. Friends who like it will like it and follow it. Your support is the greatest encouragement to me. Come on, everyone!
This is the article about SpringMVC parameter delivery skills and practical guide (full case). For more relevant SpringMVC parameter delivery content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!