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

I have a spring roo web app running in tomcat 7. There I have a way to manage the generated spring roome I pushed for debug problems:

@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 you can see in the logs:

  find author lastname: ШÑ<U+0082>Γ‘<U+0080>СмбСÑ<U+0080>г
  lastnameISOtoUTF8: 

      

the requestparameters firstName and lastName are encoded as ISO-8859-1, but I expected to be UTF-8 encoded. I think I've seen most of these questions.

and make sure all limits are set for 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 clues what else needs to be done to get rid of this ridiculous ISO-8859-1 spring mvc decoding I guess ... have any ideas? what am I missing?

+3


source to share


1 answer


I had the same problem and after a lot of research it turned out it was a Tomcat connector. I tried everything but only add the following fixes to "server.xml"

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

      



It's a shame that you can't install something in Spring to install URIEncoding.

Also, the update initially <connector ... URIEncoding="UTF-8"/>

didn't work, but that was because my Tomcat "server.xml" was overridden, so make sure it doesn't.

+1


source







All Articles