![](/img/trans.png)
[英]415 Unsupported Media Type (org.springframework.web.client.HttpClientErrorException)
[英]org.springframework.web.client.HttpClientErrorException: 415 null(Spring Resttemplate)
我嘗試使用rest模板獲取實體列表,但在行中卻收到415錯誤:
ResponseEntity<List<ResponseOrderDto>> responseEntity = rest.exchange
我的實現:
RestTemplate rest = new RestTemplate();
rest.getInterceptors().add((request, body, execution) -> {
ClientHttpResponse response = execution.execute(request,body);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
response.getHeaders().add("Bearer", contentToken);
return response;
});
ResponseEntity<List<ResponseOrderDto>> responseEntity = rest.exchange(
ORDER_SERVICE_URL + "/by-user",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<ResponseOrderDto>>() {
});
如何解決此錯誤?
415表示不支持的內容類型,因此Content-type標頭設置不正確。
根據您的代碼,我認為請求需要JSON Content-Type標頭,但是您在響應而不是請求上設置了Content-Type標頭。
將攔截器更改為以下形式:
rest.getInterceptors().add((request, body, execution) -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add("Bearer", contentToken);
ClientHttpResponse response = execution.execute(request, body);
return response;
});
在正確的時間為請求設置標題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.