在我正在使用的环境(Tomcat 6)中,路径段中的百分比序列显然在映射到@PathVariable时使用ISO-8859-1解码。Spring/Rest @PathVariable字符编码
我希望这是UTF-8。
我已将Tomcat配置为使用UTF-8(使用server.xml中的URIEncoding属性)。
Spring/Rest是否自行解码?如果是,我可以在哪里覆盖默认编码?
其他信息;这里是我的测试代码:
@RequestMapping(value = "/enc/{foo}", method = RequestMethod.GET)
public HttpEntity<String> enc(@PathVariable("foo") String foo, HttpServletRequest req)
{
String resp;
resp = " path variable foo: " + foo + "\n" +
" req.getPathInfo(): " + req.getPathInfo() + "\n" +
"req.getPathTranslated(): " + req.getPathTranslated() + "\n" +
" req.getRequestURI(): " + req.getRequestURI() + "\n" +
" req.getContextPath(): " + req.getContextPath() + "\n";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("text", "plain", Charset.forName("UTF-8")));
return new HttpEntity<String>(resp, headers);
}
如果我做了以下URI路径的HTTP GET请求:
/TEST/enc/%c2%a3%20and%20%e2%82%ac%20rates
这是UTF-8编码,然后百分比编码的
/TEST/enc/£ and € rates
形式
我得到的输出是:
path variable foo: £ and ⬠rates
req.getPathInfo(): /enc/£ and € rates
req.getPathTranslated(): C:\Users\jre\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TEST\enc\£ and € rates
req.getRequestURI(): /TEST/enc/%C2%A3%20and%20%E2%82%AC%20rates
req.getContextPath(): /TEST
对我来说,这表明Tomcat(在设置URIEncoding属性后)做了正确的事情(请参阅getPathInfo()),但路径变量仍在ISO-8859-1中解码。
答案是:
春/休息显然使用请求编码,这是一件非常奇怪的事情,因为这是关于体,而不是URI。叹。
添加此:
<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>
解决了这一问题。它确实应该更简单。
而实际上,它更糟糕的是:
如果方法确实有请求主体,而在UTF-8,一个是没有编码,需要附加forceEncoding参数。这似乎有效,但我担心它会在以后引发更多问题。
另一种方法
在此期间,我发现它可以禁用解码,我指定
<property name="urlDecode" value="false"/>
...在这种情况下,收件人可向正确的事;但当然这会使很多其他事情变得更加困难。
这在理论上听起来不错,但似乎没有帮助。查看文档,如果强制执行* body *的编码,而不是URI。 – 2010-12-17 15:41:36
@Julian:这是一个正确的解决方案(尽管'forceEncoding'没有必要),Spring使用请求编码来解析路径变量,参见http://static.springsource.org/spring/docs/3.0.x/javadoc-api /org/springframework/web/util/UrlPathHelper.html(无论如何你也需要这个POST参数过滤器)。 – axtavt 2010-12-19 18:51:32
@axtavt:哦,我的,谁想出这样的设计?无论如何,当我使用UTF-8编码体(如POST)发送HTTP请求时,我已经能够确认我确实获得了UTF-8。我*无法*得到过滤器工作广告(我知道发生了什么事情,因为当我打破类名时,我得到一个ClassNotFoundException)。 – 2010-12-19 19:45:06