Spring/Rest@PathVariable字符编码
在我使用的环境(Tomcat 6)中,路径段中的百分比序列显然在映射到@PathVariable时使用ISO-8859-1进行解码 我希望是UTF-8 我已经将Tomcat配置为使用UTF-8(使用server.xml中的URIEncoding属性) Spring/Rest是否独立进行解码?如果是,在哪里可以覆盖默认编码 补充资料;以下是我的测试代码:Spring/Rest@PathVariable字符编码,spring,rest,character-encoding,uri,Spring,Rest,Character Encoding,Uri,在我使用的环境(Tomcat 6)中,路径段中的百分比序列显然在映射到@PathVariable时使用ISO-8859-1进行解码 我希望是UTF-8 我已经将Tomcat配置为使用UTF-8(使用server.xml中的URIEncoding属性) Spring/Rest是否独立进行解码?如果是,在哪里可以覆盖默认编码 补充资料;以下是我的测试代码: @RequestMapping( value = "/enc/{foo}", method = RequestMethod.GET ) publ
@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 );
}
这是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()),但path变量仍然在ISO-8859-1中解码
答案是:
Spring/Rest显然使用了请求编码,这是一件非常奇怪的事情,因为这是关于主体的,而不是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>
字符编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
字符编码滤波器
/*
解决了这个问题。它真的应该更简单
事实上,情况更糟:
如果该方法确实有一个请求体,而该请求体没有用UTF-8编码,则需要额外的forceEncoding参数。这似乎是可行的,但我担心以后会引起更多的问题
另一种方法
同时,我发现可以禁用解码,我的
<property name="urlDecode" value="false"/>
…在这种情况下,收件人可以选择正确的对象;当然,这会使很多其他事情变得更加困难。我想你需要在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>
字符编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
字符编码滤波器
/*
对于我来说,path变量仍然在ISO-8859-1中解码,即使使用字符编码过滤器也是如此。这是我要做的,来绕开这件事。如果你还有其他想法,请告诉我
要查看服务器上实际的UTF-8解码字符,只需执行此操作并查看值(需要将“HttpServletRequest HttpServletRequest”添加到控制器参数中):
现在我在服务器上有了正确的解码数据,我就可以做我想做的任何事情(比如从解码的URI手动获取参数)。但是,要让这一切正常工作,你必须搞乱Tomcat配置(Uricodencing),这难道不是很糟糕吗?如果ServletAPI提供了一种在未编码的表示中获取路径和请求参数的方法,那么应用程序(或Spring)可以完全自行处理解码。显然,
HttpServletRequest#getPathInfo
和HttpServletRequest#getQueryString
甚至可以提供这一功能,但对于后者,这意味着Spring必须解析和解码查询字符串本身,而不依赖于HttpServletRequest#getParameter
和friends。显然他们没有这样做,这意味着您不能让@RequestParam
或@PathVariable
在不依赖servlet容器配置的情况下安全地捕获除us ascii字符串以外的任何内容。尝试在server.xml中的Tomcat上配置连接器。
将useBodyEncodingForURI=“true”
或URIEncoding=“UTF-8”
添加到连接器标记中。
例如:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
useBodyEncodingForURI="true"
redirectPort="8443" />
这在理论上听起来不错,但似乎没有帮助。查看文档,如果强制对正文进行编码,而不是URI。@朱利安:这是一个正确的解决方案(虽然forceEncoding
不是必需的),Spring使用请求编码来解析路径变量,请参阅(而且您还需要对POST参数使用此筛选器)。@axtavt:噢,天哪,谁想出了这样的设计?无论如何,当我发送一个带有UTF-8编码体(如POST)的HTTP请求时,我已经能够确认我确实得到了UTF-8。我无法使筛选器按广告的方式工作(我知道发生了一些事情,因为当我破坏类名时,我得到一个ClassNotFoundException)。@axtavt:噢,我缺少筛选器映射元素。它不工作。SpringMVC4.0.2完全注释。无论如何,URL都是在ISO-8859-1中解码的。我必须准备变通办法。必须首先使用字节[]字节=var.getBytes(“ISO-8859-1”)解码路径变量,例如@PathVariable String var
编码>然后用新字符串(字节,“UTF-8”)以UTF-8编码代码>。确保分派servlet的URL映射不短于CharacterEncodingFilter,否则它甚至不会命中筛选器。这就是问题所在!谢谢
String requestURI = httpServletRequest.getRequestURI();
String decodedURI = URLDecoder.decode(requestURI, "UTF-8");
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
useBodyEncodingForURI="true"
redirectPort="8443" />