I have a controller that provides RESTful access to information:
@RequestMapping(method = RequestMethod.GET, value = Routes.BLAH_GET + "/{blahName}")
public ModelAndView getBlah(@PathVariable String blahName, HttpServletRequest request,
HttpServletResponse response) {
The problem I am experiencing is that if I hit the server with a path variable with special characters it gets truncated. For example: http://localhost:8080/blah-server/blah/get/blah2010.08.19-02:25:47
The parameter blahName will be blah2010.08
However, the call to request.getRequestURI() contains all the information passed in.
Any idea how to prevent Spring from truncating the @PathVariable?
adding the ":.+" worked for me, but not until I removed outer curly brackets.
value = {"/username/{id:.+}"}
didn't workvalue = "/username/{id:.+}"
worksHope I helped someone :]
Using the correct Java configuration class :
My preferable solution to prevent the Spring MVC @PathVariable to get truncated is to add trailing slash at the end of the path variable.
For example:
So, the request will look like:
if you are sure that your text will not match any of default extensions you can use below code:
I resolved by this hack
1) Added HttpServletRequest in @PathVariable like below
2) Get the URL directly (At this level no truncation) in the request
Spring MVC @PathVariable with dot (.) is getting truncated
Spring considers that anything behind the last dot is a file extension such as
.json
or.xml
and truncate it to retrieve your parameter.So if you have
/{blahName}
:/param
,/param.json
,/param.xml
or/param.anything
will result in a param with valueparam
/param.value.json
,/param.value.xml
or/param.value.anything
will result in a param with valueparam.value
If you change your mapping to
/{blahName:.+}
as suggested, any dot, including the last one, will be considered as part of your parameter:/param
will result in a param with valueparam
/param.json
will result in a param with valueparam.json
/param.xml
will result in a param with valueparam.xml
/param.anything
will result in a param with valueparam.anything
/param.value.json
will result in a param with valueparam.value.json
If you don't care of extension recognition, you can disable it by overriding
mvc:annotation-driven
automagic:So, again, if you have
/{blahName}
:/param
,/param.json
,/param.xml
or/param.anything
will result in a param with valueparam
/param.value.json
,/param.value.xml
or/param.value.anything
will result in a param with valueparam.value
Note: the difference from the default config is visible only if you have a mapping like
/something.{blahName}
. See Resthub project issue.If you want to keep extension management, since Spring 3.2 you can also set the useRegisteredSuffixPatternMatch property of RequestMappingHandlerMapping bean in order to keep suffixPattern recognition activated but limited to registered extension.
Here you define only json and xml extensions:
Note that mvc:annotation-driven accepts now a contentNegotiation option to provide a custom bean but the property of RequestMappingHandlerMapping has to be changed to true (default false) (cf. https://jira.springsource.org/browse/SPR-7632).
For that reason, you still have to override all the mvc:annotation-driven configuration. I opened a ticket to Spring to ask for a custom RequestMappingHandlerMapping: https://jira.springsource.org/browse/SPR-11253. Please vote if you are interested in.
While overriding, be careful to consider also custom Execution management overriding. Otherwise, all your custom Exception mappings will fail. You will have to reuse messageCoverters with a list bean:
I implemented, in the open source project Resthub that I am part of, a set of tests on these subjects: see https://github.com/resthub/resthub-spring-stack/pull/219/files and https://github.com/resthub/resthub-spring-stack/issues/217