4

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

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
James Zalame
  • 183
  • 2
  • 11

1 Answers1

7

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Having the exact same problem, but the accepted answer did not work for me. The query string is getting encoded in utf-8 (same as body) but for some reason the decoding fails. When debbuging I can see my CharacterEncodingFilter right next to ApplicationFilterChain. Is this the correct order? I'm not using web.xml though. Should I open a new question? – pakman Jun 03 '14 at 15:18
  • 1
    @pakman `CharacterEncodingFilter` decodes the body of the request, not the query string. Have you setup the connector? – Sotirios Delimanolis Jun 03 '14 at 15:20
  • Yes, I tried both useBodyEncodingForURI="true" and URIEncoding="UTF-8" (not at the same time) ... and nothing. I have tried with several tomcat 7 versions, including latest tomcat 7 (v. 7.0.54). It is driving me crazy. – pakman Jun 03 '14 at 16:03
  • @pakman I guess you should open a new question with the relevant details. Please link it here and I'll try to take a look. – Sotirios Delimanolis Jun 03 '14 at 16:04
  • 1
    Got it! My dev. setup is rather involved, requiring an apache web server connecting to tomcat, so I was modifying the wrong . I was modifying the standard http connector instead of the AJP connector. It now works fine with URIEncoding="UTF-8"... thank you very much for your comments, you kept me thinking. – pakman Jun 03 '14 at 16:07