I have annotated mappings working great through my spring mvc web app, however, they are case sensitive. I cannot find a way to make them case insensitive. (I'd love to make this happen within Spring MVC, rather than redirecting traffic somehow)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Example from a bean file in Spring 4.2 and this is ONLY SUPPORTED v4.2+:
Well, I can't answer your question (I tried, I thought I could figure it out). But seeing as you haven't received any responses in 2 days, here are some leads at least:
This example seems to suggest it's possible:
http://webcache.googleusercontent.com/search?q=cache:ELj-ZQ8G4z0J:www.springbyexample.org/examples/sdms-simple-spring-mvc-web-module.html+case+insensitive+requestmapping+spring&cd=3&hl=en&ct=clnk&client=firefox-a
It references this class in Spring
http://static.springsource.org/spring/docs/3.0.4.RELEASE/javadoc-api/org/springframework/web/servlet/mvc/support/ControllerClassNameHandlerMapping.html
My guess (and it's just that, a guess), is that you need to expand
<mvc:annotation-driven/>
and implement the individual beans with the correct parameters to make it case-insensitive. See:http://rapid-web.tumblr.com/post/296916668/what-does-annotation-driven-do
A last note, I noticed somewhere else in reading that it said that all paths default to lower case, have you verified that
/MyPath
isn't handled by@RequestMapping("/mypath")
?Again, just food for thought as best I can do. Maybe it'll get you far enough along to ask a more specific question that leads you to the answer - that's how these things work sometimes. Good luck!
Spring 4.2 will support case-insensitive path matching. You can configure it as follows:
In Spring 3.2+ / Spring Boot, you can now set up case insensitive URL matching using the simplified Java config.
First you need to create the CaseInsensitivePathMatcher.groovy or Java class:
Next, to make this happen, you should have a class annotated with Springs @Configuration that extends the WebMvcConfigurerAdapter class as shown below (Note that my code is contained within .groovy classes):
Then add the following 2 methods to the class:
That's it, you should now be all setup for case insensitive URL's with minimal configuration
According to this webpost you need to add both a HandlerMapping and a HandlerAdapter in Spring MVC. The Mapping maps the request to a corresponding controller, and the adapter is responsible to execute the request using the controller.
You therefore need to override the PathMatcher for both the mapper and adapter.
Ex (will make all @Controllers case-insensitive):
New Matcher:
applicationContext.xml:
Added about the same that <mvc:annotation-driven> would do. (Thanks to David Parks for the link)
Problem report for solution by smat
In solution by smat, there is one little side-effect (I would blame spring-mvc for that).
At first,
AntPathMatcher.doMatch()
seems to return true/false depending on requested-url and controller-method's request-mapping string (That's the only thing should be done here). But, this method is used for one more purpose as well (which is not written in documentation!). Another purpose is to collect corresponding values for@PathVariable
in controller-method. These values are collected inMap<String, String> uriTemplateVariables
(last parameter).And these collected values are used to pass to controller-method as parameter value.For example, we have controller-method like this,
and if we access with URL,
/code/AbD
then with solution by smatAntPathMatcher.doMatch()
will collect@PathVariable
value inMap<String, String> uriTemplateVariables
asuserCode->abd
. As we are lower-casing the path string, values collected are also lower-cased. And this lower-cased userCode value is passed to our controller.But, I am thankful to solution by smat which served me well so far without any other problems.
Solution
Solved this problem by doing work around solution by smat. Without lower-casing path or pattern string in extended
AntPathMatcher
class, I forced my extendedAntPathMatcher
to use my customAntPathStringMatcher
. my customAntPathStringMatcher
does case-insesitive matching without changing the case of actual string.In following solution code most of the code is copied from original class code(code I wanted to customize was hidden for subclass because of private access).
Custom AntPathMatcher,
}
Custom AntPathStringMatcher,