What's the difference between “URIEncoding” of

2019-03-28 17:34发布

问题:

There may be many ways to solve encoding problem:

  • Encoding filter like Spring MVC UTF-8 Encoding

  • Setting URIEncoding=UTF-8 in server.xml of Tomcat , like http://struts.apache.org/release/2.1.x/docs/how-to-support-utf-8-uriencoding-with-tomcat.html.

  • request.setCharacterEncoding( utf-8 )

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!

回答1:

  • 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