相关疑难解决方法(0)

Spring Rest 模板无法从 text/html 转换

春季休息模板引发了以下异常

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [Lcom.flightsms.core.dto.AirlineResponseDTO;] and content type [text/html;charset=UTF-8]
Run Code Online (Sandbox Code Playgroud)

这是我的 json 响应

[
 {
  "airlineId": "1",
  "nameAirline": "American Airlines",
  "codeIataAirline": "AA",
  "iataPrefixAccounting": "1",
  "codeIcaoAirline": "AAL",
  "callsign": "AMERICAN",
  "type": "scheduled",
  "statusAirline": "active",
  "sizeAirline": "963",
  "ageFleet": "10.9",
  "founding": "1934",
  "codeHub": "DFW",
  "nameCountry": "United States",
  "codeIso2Country": "US"
 }
]
Run Code Online (Sandbox Code Playgroud)

dto类

@Data
public class AirlineResponseDTO {
  private String airlineId;
  private String nameAirline;
  private String codeIataAirline;
  private String iataPrefixAccounting;
  private String codeIcaoAirline;
  private String callsign; …
Run Code Online (Sandbox Code Playgroud)

rest spring json resttemplate

7
推荐指数
2
解决办法
1万
查看次数

带有自定义标头的 Resttemplate GET 请求

我需要发送一个头的GET请求:Content-Type: application/camiant-msr-v2.0+xml。我期望来自服务器的 XML 响应。我用 Postman 测试了请求和响应,一切都很好。但是当我尝试在 Spring 中使用 时RestTemplate,我总是收到 400 个错误的请求。例外情况spring是:

Jul 09, 2016 12:53:38 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/smp] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 Bad Request] with root cause
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
Run Code Online (Sandbox Code Playgroud)

我的代码:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Content-Type", "application/camiant-msr-v2.0+xml");

HttpEntity<?> entity = new HttpEntity<Object>(headers);
log.debug("request headers: …
Run Code Online (Sandbox Code Playgroud)

rest spring custom-headers resttemplate

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

rest ×2

resttemplate ×2

spring ×2

custom-headers ×1

json ×1