Issue
I'm trying to add a matrix parameter (or matrix variable) to my Rest Controller using SpringMVC (from Spring boot 1.2.3.RELEASE) Here is my code :
@RestController
public class SubAgentsController {
@RequestMapping(value = "/{subagents}", method = RequestMethod.GET)
public SubAgent subAgents(@MatrixVariable(value="agentName", pathVar="subagents") String agentName) {
System.out.println(agentName);
}
}
Unfortunately, when I try to get : http://localhost:8080/subagents;agentName=hello
that is the answer I receive :
There was an unexpected error (type=Bad Request, status=400).
Missing matrix variable 'agentName' for method parameter of type String
What did I do wrong ? According to http://docs.spring.io/spring-framework/docs/3.2.0.M2/reference/html/mvc.html that should work :-(
Thanks for your answers!
Solution
As the documentation you linked to states,
Note that to enable the use of matrix variables, you must set the
removeSemicolonContent
property ofRequestMappingHandlerMapping
tofalse
. By default it is set totrue
with the exception of the MVC namespace and the MVC Java config both of which automatically enable the use of matrix variables.
If you're configuring your application by extending WebMvcConfigurationSupport
, then override the requestMappingHandlerMapping
method which prepares the RequestMappingHandlerMapping
and set its appropriate property.
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
final RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping();
requestMappingHandlerMapping.setRemoveSemicolonContent(false); // <<< this
return requestMappingHandlerMapping;
}
You'll then be all set.
With Spring Boot, I think all you need is to declare a @Bean
method with the above, ie. that returns a RequestMappingHandlerMapping
instance.
Answered By - Sotirios Delimanolis
Answer Checked By - Willingham (JavaFixing Volunteer)