在Spring Framework中使用@PathVariable时编码不正确

时间:2014-05-11 20:50:08

标签: java spring tomcat spring-mvc

在我的控制器中,我有这个方法:

@RequestMapping(value = "/find-by-company-name/{companyName}", method = RequestMethod.GET)
public CustomerDto findByCompanyName(@PathVariable String companyName){
    return customerService.findByCompanyName(companyName);
}

当我使用此/find-by-company-name/Mestská%20knižnica之类的网址时,我会在Mestsk?? kni??nica变量中获得companyName

这个问题有解决方法吗?

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>

2 个答案:

答案 0 :(得分:3)

解决方案是将URIEncoding="UTF-8"添加到 server.xml 配置文件中,如下所示:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" 
           URIEncoding="UTF-8" />

答案 1 :(得分:0)

您的网址应该被编码,就像空格%20一样。然后编码就可以了。