Spring4 MVC如何接收GET请求中的分号分隔参数
Great question! The semicolon-separated parameters in your URL are called Matrix Variables in Spring MVC, and here's how you can handle them in your Spring 4 REST controller:
Step 1: Enable Matrix Variables Support
Spring 4 disables matrix variable handling by default (it automatically removes semicolon-containing content from URLs). You need to override this behavior first via configuration:
Option 1: Java-based Configuration
Create a configuration class extending WebMvcConfigurerAdapter (the standard way to customize Spring MVC in Spring 4) to enable matrix variables:
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); // Disable removing semicolon content to allow matrix variables urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }
Option 2: XML Configuration
If you're using XML-based Spring configuration, add this to your servlet context file:
<mvc:annotation-driven> <mvc:path-matching remove-semicolon-content="false"/> </mvc:annotation-driven>
Step 2: Receive Matrix Variables in Your Controller
Once configured, use the @MatrixVariable annotation to extract id1 and id2 from your URL. Here are two practical approaches:
Approach 1: Directly Extract Variables
For your example URL http://localhost:9080/myapp/v1/userdetails/{id1;id2}, the matrix variables are part of the userdetails path segment. You can extract them directly in your controller method parameters:
@RestController @RequestMapping("/myapp/v1") public class UserDetailsController { @GetMapping("/userdetails") public ResponseEntity<String> getUserDetails( @MatrixVariable("id1") String id1, @MatrixVariable("id2") String id2) { // Your business logic here (e.g., fetch user details using id1 and id2) return ResponseEntity.ok(String.format("Received id1: %s, id2: %s", id1, id2)); } }
Approach 2: Link to a Path Variable
If you prefer to define a placeholder in your path (like /userdetails/{userIds}), you can explicitly link the matrix variables to that path segment:
@RestController @RequestMapping("/myapp/v1") public class UserDetailsController { @GetMapping("/userdetails/{userIds}") public ResponseEntity<String> getUserDetails( @MatrixVariable(value = "id1", pathVar = "userIds") String id1, @MatrixVariable(value = "id2", pathVar = "userIds") String id2) { // Handle the parameters as needed return ResponseEntity.ok(String.format("id1: %s, id2: %s", id1, id2)); } }
This works for URLs like http://localhost:9080/myapp/v1/userdetails/id1=123;id2=456 or even http://localhost:9080/myapp/v1/userdetails/foo;id1=123;id2=456 (the path variable value doesn't affect matrix variable extraction).
Additional Tips
- Make variables optional: Add
required = falseto@MatrixVariableif the parameter isn't mandatory (e.g.,@MatrixVariable(value = "id1", required = false)). - Collect all matrix variables: Use
@MatrixVariable Map<String, String> matrixVarsto get all matrix variables in a single map. - Matrix variables can appear in any path segment: For example,
http://localhost:9080/myapp/v1;version=1/userdetails;id1=123;id2=456lets you extractversionas another matrix variable.
内容的提问来源于stack exchange,提问作者MiniSu

