Search code examples
javaandroidspringresttemplate

String parametern in URL on server side gets wrong decoding


When I am trying to send URL to server, it's look like: http://192.168.0.80:8080/directory/getCertainServices/1/Кузовные работы

But on the server side "Кузовные работы" parameter looks like:

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

I am using RestTemplate from spring framework to send data from client:

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

Server side code:

 @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);
    }

Can anyone give an advice what's the problem here ?

UPDATE QUESTION: Does this mean that I have to use only english words in URL path ?


Solution

  • Tomcat Wiki has a good page called "Character Encoding Issues" that describes the problem pretty well.

    The solution depends on the actual web server being used, but if it's Tomcat (default for Spring), the solution is on that wiki page:

    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.