Reputation: 15219
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]
Upvotes: 2
Views: 3947
Reputation: 1
Sorry for answer latency, I'm answering so when one searches a solution for related problem, it could be a reference.
In addition to converter answer, you should extend a ResponseExtractor with HttpMessageConverter and override getContentType
there, because RestTemplate execute
method firstly checks content type of response and if it cannot be parsed, then also throws exception there. So both Converter and Extractor should exist.
Upvotes: 0
Reputation: 1496
You implement a custom HttpMessageConverter. Here an example. I guess that you can reuse the JSON converter and register the custom content type.
Upvotes: 1