Java Spring resttemplate character encoding

后端 未结 5 499
甜味超标
甜味超标 2020-12-05 10:29

I\'m using the Java Spring Resttemplate for getting a json via a get request. The JSON I\'m getting has instead of special character slike ü ö ä or ß some weird stuff. So I

5条回答
  •  日久生厌
    2020-12-05 10:51

    I have solved this problem. I need to POST a string object in request body with UTF-8.

    text/plain

    httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));

    applicaton/json

    httpHeaders.setContentType(new MediaType("applicaton", "json", StandardCharsets.UTF_8));

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity resposeEntity = null;
    
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));
    
    HttpEntity httpEntity = new HttpEntity(stringContent, httpHeaders);
    responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
    
    if (HttpStatus.OK.equals(responseEntity.getStatusCode())) {
        logger.debug("... success ... result: " + responseEntity.getBody());
    }
    

提交回复
热议问题