How to change HTTP response header in spring RestTemplate request request?

I have a simple java spring method to create an object

RestTemplate restTemplate = new RestTemplate();
Address address = restTemplate.getForObject(url, Address.class);

But the server answers me with a JSON string with the wrong Content-Type: text / plain instead of application / json (noted in Postman). And I get an exception:

Failed to retrieve response: no suitable HttpMessageConverter was found for response type [class Address] and content type [text / plain; charset = utf-8]

So it seems to me, I need to change the header of the Content-Type response to the right application / json , so that MappingJackson2HttpMessageConverter detects the JSON string and the startup code.

+4
source share
3 answers

After spending an hour, I found a short and easy way.

The Json converter by default only supports "application / json". We simply redefine it to support "text / plain".

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

// support "text/plain"
converter.setSupportedMediaTypes(Arrays.asList(TEXT_PLAIN, APPLICATION_JSON));

RestTemplate template = new RestTemplate();
template.getMessageConverters().add(converter);

// It ok now
MyResult result = tmp.postForObject("http://url:8080/api", 
            new MyRequest("param value"), MyResult.class);
+4
source

Thanks for the help! In case I cannot change the response header. I am creating a new response object with the correct header.

            ClientHttpRequest clientHttpRequest = new SimpleClientHttpRequestFactory().createRequest(URI.create(str), org.springframework.http.HttpMethod.GET);
            final ClientHttpResponse clientHttpResponse = clientHttpRequest.execute();
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
            Address address = new Address();
            //It always true, because service always returns 200 OK
            if (clientHttpResponse.getStatusCode() == HttpStatus.OK) {
                address = (Address) converter.read(address.getClass(), new HttpInputMessage() {
                    public InputStream getBody() throws IOException {
                        return clientHttpResponse.getBody();
                    }

                    public HttpHeaders getHeaders() {
                        HttpHeaders httpHeaders = new HttpHeaders();
                        httpHeaders.putAll(clientHttpResponse.getHeaders());
                        httpHeaders.put("Content-Type", Collections.singletonList(MediaType.APPLICATION_JSON_VALUE));
                        return httpHeaders;
                    }
                });
                busStop.setNearestAddress(address.toString());
            }

I am sure this is not a simple and good solution, but it works.

+1
source

, - :

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity entity = new HttpEntity(headers);
    ResponseEntity<Address> response = restTemplate.exchange(url, HttpMethod.GET,  entity, Address.class);
    Address address = response.getBody();
0

Source: https://habr.com/ru/post/1671435/


All Articles