1

I have a problem seralizing a list of objects to JSON (Jackson). My objects contain strings with German umlaute (e.g. ä, ö, ü, ß). When I send a list with these objects to my server, I get an JsonMappingException: Invalid UTF-8 middle byte. I'm 100% the problem is on the client side, because when I send the JSON string via postman, it works just fine.

public static ResponseEntity<?> exchange(String servicePath, HttpMethod method, Class returnClass, List<? extends BaseObject> objectList, Object... uriVariableValues) throws JsonProcessingException, UnsupportedEncodingException
{
    RestTemplate template = new RestTemplate();
    template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    template.getMessageConverters().add(new StringHttpMessageConverter(Charset.forName("UTF-8")));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    String url = ConfigHandler.getServicePath(servicePath);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ConfigHandler.getStringProperty("baseUrl"));
    URI uri = builder.path(url).buildAndExpand(uriVariableValues).toUri();
    byte[] bytes = Utils.getJacksonObjectMapper().writeValueAsBytes(objectList);

    //String urlParameters = new String(Utils.getJacksonObjectMapper().writeValueAsString(objectList).getBytes("UTF-8"));
    String urlParameters = new String(bytes, "UTF-8");
    //urlParameters = urlParameters.replaceAll("ß", "HURZ");
    System.out.println("WebUtils.exchange - JSON: "+urlParameters);

    HttpEntity<String> entity = new HttpEntity<>(urlParameters, headers);

    return template.exchange(uri, method, entity, returnClass);
}

public static ObjectMapper getJacksonObjectMapper()
{
    ObjectMapper mapper = new ObjectMapper()
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module()).registerModule(new JavaTimeModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return mapper;
}

The JSON string in question is the following {"objectType":"TRANSLATION_PARAMETER","changed":true,"type":"java.lang.String","locale":"de_DE","value":"Schließen","section":null,"prettyId":"","active":true,"createdDate":"2017-10-01T13:35:52.6","name":"General.close","id":0,"description":" "}

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
Abenstex
  • 100
  • 9

2 Answers2

1

In case somebody stumbles upon this question in the future, the solution was the adding a little bit more to the HttpHeaders:

HttpHeaders headers = new HttpHeaders();
MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
headers.setContentType(mediaType);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));
Abenstex
  • 100
  • 9
0

Set the correct encoding in your client side:

updateRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity entity= new StringEntity(json, "UTF-8");
updateRequest.setEntity(entity);
ALUFTW
  • 1,914
  • 13
  • 24