Spring/Rest @PathVariable 字符编码 [英] Spring/Rest @PathVariable character encoding

查看:36
本文介绍了Spring/Rest @PathVariable 字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用的环境 (Tomcat 6) 中,路径段中的百分比序列在映射到 @PathVariable 时显然是使用 ISO-8859-1 解码的.

In the environment I'm using (Tomcat 6), percent sequences in path segments apparently are decoded using ISO-8859-1 when being mapped to a @PathVariable.

我希望它是 UTF-8.

I'd like that to be UTF-8.

我已经将 Tomcat 配置为使用 UTF-8(使用 server.xml 中的 URIEncoding 属性).

I already configured Tomcat to use UTF-8 (using the URIEncoding attribute in server.xml).

Spring/Rest 是否自己进行解码?如果是,我在哪里可以覆盖默认编码?

Is Spring/Rest doing the decoding on its own? If yes, where can I override the default encoding?

附加信息;这是我的测试代码:

Additional information; here's my test code:

@RequestMapping( value = "/enc/{foo}", method = RequestMethod.GET )
public HttpEntity<String> enc( @PathVariable( "foo" ) String foo, HttpServletRequest req )
{
  String resp;

  resp = "      path variable foo: " + foo + "
" + 
         "      req.getPathInfo(): " + req.getPathInfo() + "
" +
         "req.getPathTranslated(): " + req.getPathTranslated() + "
" + 
         "    req.getRequestURI(): " + req.getRequestURI() + "
" + 
         "   req.getContextPath(): " + req.getContextPath() + "
";

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType( new MediaType( "text", "plain", Charset.forName( "UTF-8" ) ) );
  return new HttpEntity<String>( resp, headers );
}

如果我使用以下 URI 路径执行 HTTP GET 请求:

If I do an HTTP GET request with the following URI path:

/TEST/enc/%c2%a3%20and%20%e2%82%ac%20rates

这是 UTF-8 编码然后百分比编码的形式

which is the UTF-8 encoded then percent-encoded form of

/TEST/enc/£ and € rates

我得到的输出是:

      path variable foo: £ and ⬠rates
      req.getPathInfo(): /enc/£ and € rates
req.getPathTranslated(): C:Usersjreworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsTESTenc£ and € rates
    req.getRequestURI(): /TEST/enc/%C2%A3%20and%20%E2%82%AC%20rates
   req.getContextPath(): /TEST

在我看来,Tomcat(设置 URIEncoding 属性后)做了正确的事情(请参阅 getPathInfo()),但路径变量仍在 ISO-8859-1 中解码.

which to me shows that Tomcat (after setting the URIEncoding attribute) does the right thing (see getPathInfo()), but the path variable is decoded still in ISO-8859-1.

答案是:

Spring/Rest 显然使用了请求编码,这是一件非常奇怪的事情,因为这是关于 body,而不是 URI.叹气.

Spring/Rest apparently uses the request encoding, which is a very strange thing to do, as this is about the body, not the URI. Sigh.

添加:

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

解决了这个问题.确实应该更简单.

fixed the problem. It really should be simpler.

实际上,情况更糟:

如果该方法确实一个请求主体,并且该主体不是以 UTF-8 编码的,则需要额外的 forceEncoding 参数.这似乎有效,但我担心以后会引起更多问题.

If the method indeed has a request body, and that one isn't encoded in UTF-8, the additional forceEncoding parameter is needed. This seems to work, but I'm concerned it will cause more problems later on.

另一种方法

与此同时,我发现可以禁用解码,我指定

In the meantime, I found out that it's possible to disable the decoding, my specifying

<property name="urlDecode" value="false"/>

...在这种情况下,接收者可以做正确的事情;但这当然会让很多其他事情变得更难.

...in which case the recipient can to the right thing; but of course this will make lots of other things harder.

推荐答案

你需要给 web.xml 添加过滤器

I thing that you need add filter to 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>

这篇关于Spring/Rest @PathVariable 字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆