I am trying to send a GET request with query string parameter in hebrew. When the controller gets the request, the parameter is in gibberish. i've added "org.springframework.web.filter.CharacterEncodingFilter" but it didn't change a thing.
Please advise how to fix it.
Update: here is the the request.
GET /myapp/specialties?query=%D7%92%D7%99%D7%A0%D7%A0%D7%A0%D7%A0 HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: *
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/json;charsert=utf-8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,he;q=0.6
Query String Parametersview sourceview URL encoded
query:גיננננ
Response Headersview source
Content-Type:application/json;charset=UTF-8
Date:Mon, 03 Mar 2014 20:45:17 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>med.rec</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
-Roy
As it turns out HttpServletRequest#setCharacterEncoding(String)
which CharacterEncodingFilter
uses
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect.
Which is no good for you since you aren't getting the parameters from the body, but rather from the query string.
If you are using Tomcat, however, you are in luck. Tomcat has a special Connector
attribute which, when set (it's unset by default), will use that same character encoding for the query string.
That attribute is useBodyEncodingForURI
. If you open up your Tomcat servet.xml
file, you will find an element like (without the attribute)
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443" useBodyEncodingForURI="true">
</Connector>
Add the attribute and it will work as intended. Make sure you are setting it for the appropriate Connector
, HTTP in this case.
Other Servlet containers probably have some similar configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With