Spring / Rest @PathVariable字符编码 [英] Spring/Rest @PathVariable character encoding
问题描述
在我使用的环境(Tomcat 6)中,当映射到@PathVariable时,路径段中的百分比序列显然是使用ISO-8859-1解码的。
我想要的是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 +\\\
+
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请求:
/ TEST / enc /%c2%a3%20and%20%e2%82%ac%20rates
这是
的UTF-8编码然后百分比编码形式 / TEST / enc /£and€rates
get是:
路径变量foo:£和$ rate
req.getPathInfo():/ enc / £和€rate
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中解码。
/ p> Spring / Rest显然使用请求编码,这是一个非常奇怪的事情,因为这是关于 body ,而不是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>
$ b
另一种方法
在此期间,我发现可以禁用解码,我指定
property name =urlDecodevalue =false/>
...在这种情况下,收件人可以到正确的事情;
我需要在web.xml中添加过滤器
p> < 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>
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.
I'd like that to be UTF-8.
I already configured Tomcat to use UTF-8 (using the URIEncoding attribute in server.xml).
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 + "\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 );
}
If I do an HTTP GET request with the following URI path:
/TEST/enc/%c2%a3%20and%20%e2%82%ac%20rates
which is the UTF-8 encoded then percent-encoded form of
/TEST/enc/£ and € rates
the output that I get is:
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
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.
And the answer is:
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.
Adding this:
<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.
And actually, it's worse:
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.
Another approach
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.
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屋!