Search code examples
springspring-roohttp-request-parametersuriencoding

spring roo/spring mvc: how can i set uriencoding for controller method


i have a spring roo webapplication running in tomcat 7. there i have a spring roo generated controller method, that i pushed in for debugging issues:

@RequestMapping(params = "find=ByFirstNameEqualsAndLastNameEquals", method = RequestMethod.GET)
public String findAuthorsByFirstNameEqualsAndLastNameEquals(
        @RequestParam("firstName") String firstName,
        @RequestParam("lastName") String lastName,
        @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "size", required = false) Integer size,
        @RequestParam(value = "sortFieldName", required = false) String sortFieldName,
        @RequestParam(value = "sortOrder", required = false) String sortOrder,
        Model uiModel
        ) {

    System.out.println("find author lastname: " + lastName);
    String lastNameUTF8 = null;
    String firstNameUTF8 = null;
    try {
        lastNameUTF8 = new String(lastName.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("lastnameUTF8: " + lastNameUTF8);
        firstNameUTF8 = new String(firstName.getBytes("ISO-8859-1"),
                "UTF-8");
        System.out.println("lastnameISOtoUTF8: " + firstNameUTF8);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            ...

as one can see at the logs:

  find author lastname: ШÑ<U+0082>Ñ<U+0080>ембеÑ<U+0080>г
  lastnameISOtoUTF8: Штремберг

the requestparameters firstName and lastName come in encoded as ISO-8859-1, but i expected them UTF-8 encoded. i think i saw most of similar questions

and made sure to have all the confurations set to UTF-8:

web.xml:

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

tomcats server.xml:

...
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" 
          URIEncoding="UTF-8"
...
<!-- Define an AJP 1.3 Connector on port 8009 -->
 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" 
   URIEncoder="UTF-8"
/>

java arguments:

/usr/local/jdk7/bin/java -Duser.language=en -Duser.country=US -Dfile.encoding=UTF-8 /usr/local/tomcatODM_TEST/bin/bootstrap.jar:/usr/local/tomcatODM_TEST/bin/tomcat ...  org.apache.catalina.startup.Bootstrap start

also browser encoding is set to UTF-8, system LANG on server and client:

$ echo $LANG  en_US.utf8

i don't have any clue what else to do to get rid of that funny ISO-8859-1 decoding of spring mvc i guess... any ideas? what am i overlooking?


Solution

  • I had the same issue and after much research realised it was the Tomcat connector which was the issue. I tried everything but only adding the following fixed it to the "server.xml"

    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
    

    It's annoying that you can't set something in Spring to set the URIEncoding.

    Also, initially the <connector ... URIEncoding="UTF-8"/> update didn't work but it was because my Tomcat "server.xml" was being overriden so make sure that isn't the case.