[英]Resttemplate url response giving Invalid mime type
我正在调用 url,如下所示,但在响应中我得到了 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() ;
}
从 postman 我检查了 api 的响应标头,我们得到 Content-Type:JSON; charset=utf-8,我在这里。 如何处理?,我们只想要简单的 json 响应。
使用“@RestController”注解
@Himanshu 请提供完整的 class 信息,其中包含 class 级别注释。 除了这个问题之外,您在调用显示的 api 或 restTemplate.exchange 时是否遇到异常? . 使用完整的堆栈跟踪更新您的答案以找到它。
我更喜欢使用默认为Json响应的 @GetMapping。 请尝试一下
您可以使用 RestTemplate 拦截器完全控制请求/响应参数:
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;
}
}
在代码中:
...
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(List.of(new MimeInterceptor());
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.