Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 321
Naor Avatar asked Dec 27 '25 16:12

Naor


2 Answers

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

like image 105
nickdos Avatar answered Dec 30 '25 06:12

nickdos


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

like image 24
sinuhepop Avatar answered Dec 30 '25 04:12

sinuhepop