【问题标题】:String parametern in URL on server side gets wrong decoding服务器端 URL 中的字符串参数解码错误
【发布时间】:2017-06-04 17:09:19
【问题描述】:

当我尝试将 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 路径中只使用英文单词?

【问题讨论】:

    标签: java android spring resttemplate


    【解决方案1】:

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

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

    如何更改 GET 参数的解释方式?

    Tomcat 将使用 ISO-8859-1 作为整个 URL 的默认字符编码,包括查询字符串(“GET 参数”)(尽管请参阅下面的 Tomcat 8 通知)。

    有两种方法可以指定如何解释 GET 参数:

    • 将 server.xml 中元素的 URIEncoding 属性设置为特定的内容(例如 URIEncoding="UTF-8")。

    • 将 server.xml 中元素的useBodyEncodingForURI 属性设置为true。这将导致连接器将请求正文的编码用于 GET 参数。

    在从 8.0.0 开始的 Tomcat 8(具体来说是 8.0.0-RC3)中,元素上URIEncoding 属性的默认值取决于“严格的 servlet 合规性”设置。 URIEncoding 的默认值(关闭严格合规)现在是 UTF-8。如果启用“严格 servlet 合规性”,则默认值为 ISO-8859-1

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2019-11-11
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多