服务器端 URL 中的字符串参数解码错误

String parametern in URL on server side gets wrong decoding

当我尝试将 URL 发送到服务器时,它看起来像: http://192.168.0.80:8080/directory/getCertainServices/1/Кузовные 拼音

但在服务器端“Кузовные работы”参数看起来像:

ÐÑзовнÑе ÑабоÑÑ

我正在使用 spring 框架中的 RestTemplate 从客户端发送数据:

@Override
        protected Service[] doInBackground(Object... voids) {
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            return restTemplate.getForObject(customURL, Service[].class);
        }

服务器端代码:

 @RequestMapping(value = "/getCertainServices/{autoServiceId}/{serviceCategory}", method = RequestMethod.GET)
    @ResponseBody
    public List<Object> getService(@PathVariable("autoServiceId") Long autoServiceId, @PathVariable("serviceCategory") String serviceCategory){
        return dataBaseServiceService.findByAutoServiceAndCategory(autoServiceId, serviceCategory);
    }

任何人都可以提供建议这是什么问题吗?

更新问题: 这是否意味着我必须在 URL 路径中只使用英文单词?

Tomcat Wiki 有一个很好的页面叫做“Character Encoding Issues”,它很好地描述了这个问题。

解决方案取决于实际使用的 Web 服务器,但如果它是 Tomcat(Spring 的默认值),则解决方案在该 wiki 页面上:

How do I change how GET parameters are interpreted?

Tomcat will use ISO-8859-1 as the default character encoding of the entire URL, including the query string ("GET parameters") (though see Tomcat 8 notice below).

There are two ways to specify how GET parameters are interpreted:

  • Set the URIEncoding attribute on the element in server.xml to something specific (e.g. URIEncoding="UTF-8").

  • Set the useBodyEncodingForURI attribute on the element in server.xml to true. This will cause the Connector to use the request body's encoding for GET parameters.

In Tomcat 8 starting with 8.0.0 (8.0.0-RC3, to be specific), the default value of URIEncoding attribute on the element depends on "strict servlet compliance" setting. The default value (strict compliance is off) of URIEncoding is now UTF-8. If "strict servlet compliance" is enabled, the default value is ISO-8859-1.