6

There may be many ways to solve encoding problem:

Today, I have a problem that path param is not decoded well like

@ResponseBody
@RequestMapping(value="/context/method/{key}",method=RequestMethod.GET,produces = "application/json;charset=utf-8")
public String method(@PathVariable String key){

    logger.info("key="+key+"------------");
}

I can see that the key is decoded bad! If I pass a word "新浪" from the front end, it will become "æ°æµª". I write the below code to examine if the server is decoding this with "ISO-8859-1":

public static void main(String args[]) throws UnsupportedEncodingException{
    String key="新浪";
    byte[] bytes=key.getBytes("UTF-8");
    String decode=new String(bytes,"ISO-8859-1");
    System.out.println(decode);
}

And it comes out with the same output "æ°æµª". so indeed, the path variable is decoded with ISO-8859-1.

And then I try to add a filter to my web.xml to solve this problem:

  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping> 

But the same garbled.

Until I set below to my server.xml

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"
           URIEncoding="UTF-8" useBodyEncodingForURI="true"   ----Here is Added
/>

And it works for this even I remove the filter.

But I am still very confusing about the encoding issue. And besides , this is only GET method, if it is POST method, I guess the solution will probably be different

Can anybody please explain that what difference encoding solution should we take for what kind of problem ?

Thank you!

Vadzim
  • 24,954
  • 11
  • 143
  • 151
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149

2 Answers2

3
  • CharacterEncodingFilter configures encoding of request body. That is, it affects encoding of POST request parameters, etc, but doesn't affect encoding of GET parameters

  • URIEncoding is used to specify encoding of the URI, therefore it affects GET parameters

  • useBodyEncodingForURI="true" tells Tomcat to use encoding configured for request body when decoding URIs. So, as far as I understand, if you set CharacterEncodingFilter and useBodyEncodingForURI="true" then you don't need URIEncoding.

In practice, you need to two things to solve possible problems with encoding of parameters:

  • CharacterEncodingFilter for POST requests

  • URIEncoding (or useBodyEncodingForURI="true") for GET requests

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1.what about `request.setCharacterEncoding` 2. I read a book that in `GET` method, the pathvar and query string(URL) is decoded separately. Therefore, we need , and URIEncoding is for URI(path var), and `useBodyEncodingForURI` is for query string on URL..Is this book wrong? – JaskeyLam Nov 15 '14 at 13:39
0

for tomcat7

  • CharacterEncodingFilter configures encoding of request body. That is, it affects encoding of POST request parameters, etc, but doesn't affect encoding of GET parameters

  • useBodyEncodingForURI="true" tell tomcat to use CharacterEncoding( can be set by CharacterEncodingFilter) to parse QueryString. it's affect GET request parameter .

  • URIEncoding to parset URI , default is ISO-8859-1.

for tomcat8

  • default UTF-8.
shun chen
  • 38
  • 6