Naor
Naor

Reputation: 24063

spring mvc path variables encoding

I am using spring mvc. I am surfing to:

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

notice that פת is in hebrew and not english.
My controller is:

@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 that the controller receives פת and not the correct characters.
How can I fix it?

Even if I surfing to: http://localhost:8080/services/cities/%D7%A4%D7%AA.html I get this problem.

Upvotes: 3

Views: 4306

Answers (3)

nickdos
nickdos

Reputation: 8414

If you are using tomcat, then you have to specify the URL encoding for requests by adding URIEncoding="UTF-8" on <Connector> in the Tomcat server.xml config, as described here:

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

Upvotes: 5

sinuhepop
sinuhepop

Reputation: 20307

Here you have an interesting documentation about encoding requests in Tomcat

Something like CharacterEncodingFilter will only work in POST requests. You need to change Tomcat (or another container) configuration in order to use an URI enconding different from ISO-8859-1. But this won't work in all browsers, it depends on how they encode the URI too.

So my recommendation is always use simplest characters as possible. If you tell us what are you trying to achieve maybe we can find some better solution (I suppose you don't need the user too type the name of the city directly in address bar).

Upvotes: 1

Teja Kantamneni
Teja Kantamneni

Reputation: 17472

Use the CharacterEncodingFilter filter...

<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>

Upvotes: 0

Related Questions