I am callling a url like below, but in the response i am getting the Invalid mime type "JSON; charset=utf-8": does not contain '/'
@RequestMapping("/")
public String hello(){
HttpHeaders headers = new HttpHeaders();
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://XXXX/Query.ashx");
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<String> response = restTemplate.exchange(
builder.toUriString(),
HttpMethod.GET,
entity,
String.class);
return response.getBody() ;
}
From postman i checked the response headers of the api, there we are getting Content-Type: JSON; charset=utf-8, which i am getting here. How to handle this?, we we just want plain json response.
Use '@RestController' annotation
@Himanshu Kindly provide complete class information which contain class level annotations. Along with that question is Are you getting exception while calling shown api or restTemplate.exchange? . Update your answer with complete stacktrace to find that.
I would prefer to use @GetMapping which is default to Json Response. Please try that
You can completely control request/response parameters using RestTemplate interceptor(s):
public class MimeInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
final ClientHttpRequestExecution execution) throws IOException {
request.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.<what you want>);
ClientHttpResponse response = execution.execute(request, body);
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.<what you want>);
return response;
}
}
And in code:
...
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(List.of(new MimeInterceptor());
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.