gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:58:50 25 4
gpt4 key购买 nike

当我尝试将 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.

关于java - 服务器端 URL 中的字符串参数解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com