Force Spring RestTemplate to ignore response's content type headers

Viewed 2084

I'm trying to make a GET request to a server that I don't control, and which is returning an invalid value in the Content-Type header: "Application/json; charset=".

When I try to do a GET call, Spring throws a InvalidMimeTypeException when trying to parse this value because it rightly expects that there should be an actual charset following that = sign.

restTemplate.getForObject(new URI(DATASOURCE), String.class); // throws InvalidMimeTypeException when attempting to parse response

Is it possible to configure RestTemplate to ignore whatever content-type headers are returned by in the response? I have no control over the server I'm querying. (Spring-Boot 2.2.0.RELEASE.)

org.springframework.http.InvalidMediaTypeException: Invalid mime type "Application/json; charset=": 'value' must not be empty
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:574) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:966) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:135) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:92) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:336) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
1 Answers

You implement a custom HttpMessageConverter. Here an example. I guess that you can reuse the JSON converter and register the custom content type.

Related