Spring encoding mvc path variables

I am using spring mvc. I surf:

http://localhost:8080/services/cities/פת.html

      

please note that פת

is in Hebrew, not English.
My controller:

@RequestMapping(value="/services/cities/{name}", method=RequestMethod.GET)
public @ResponseBody List<SelectElement> getCities(@PathVariable String name) {
    List<SelectElement> elements=null;
    ...
    ...
    return elements;
}

      

The problem is the controller is receiving פת

and not the correct characters.
How can I fix this?

Even though I surf before: http://localhost:8080/services/cities/%D7%A4%D7%AA.html

I get this problem.

+3


source to share


3 answers


If you are using tomcat you need to specify the url encoding for requests by adding URIEncoding="UTF-8"

in <Connector>

to your Tomcat config server.xml

as described here:



http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8

+5


source


Here you have an interesting documentation of coding queries to Tomcat

Something like CharacterEncodingFilter will only work on POST requests. You need to change your Tomcat (or other container) configuration to use a URI other than ISO-8859-1. But this won't work in all browsers, it depends on how they encode the URIs too.



Therefore, my recommendation is always used as simply as possible. If you tell us what you are trying to achieve, maybe we can find a better solution (I suppose you don't need the user to also type the city name directly in the address bar).

+1


source


Use a filter CharacterEncodingFilter

...

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</init-param>
<init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
</init-param>

      

0


source







All Articles