我正在尝试使用本教程使用RESTful Web Service: Consuming a RESTful Web Service
然而,当调用restTemplate时,我得到以下错误
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [cukamart.cvut.fel.cz.dto.Flight;] and content type [text/html;charset=UTF-8]
我不明白为什么内容类型设置为 text / html 。它应该是 application / json ,因为我将 jackson 依赖项添加到我的pom.xml中,而spring-boot应该为我配置它吗?
因为Jackson JSON处理库在类路径中,所以RestTemplate将使用它(通过消息转换器)来转换传入的JSON数据
这是我的restTemplate bean
@Configuration
public class AosClientConfig extends WebMvcConfigurerAdapter{
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
带有jackson依赖性的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cukamart.cvut.fel.cz</groupId>
<artifactId>aosclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>aosclient</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
尝试使用RESTful Web服务的控制器
@Controller
@RequestMapping(value = "/flight")
public class FlightController {
@Value("${endpoint.url}")
private String url;
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/preview")
public String showAllFlights() {
restTemplate.getForObject(url, Flight[].class); //throw exception
....
return "flights";
}
}
知道我做错了什么或如何告诉restTemplate使用内容类型的应用程序/ json?
我也试过这个,但我得到了同样的错误
@GetMapping(value = "/preview")
public String showAllFlights() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
restTemplate.exchange(url, HttpMethod.GET, entity, Flight[].class);
return "flights";
}
答案 0 :(得分:1)
它看起来像&#34; Content-Type&#34;未设置响应中的标头。您可以使用Postman(Chrome扩展程序)或类似的REST客户端向该网址发送请求吗?这将帮助您查看REST端点是否实际使用&#34; Content-Type:application / json&#34;响应标题。
http://blog.getpostman.com/2015/06/13/debugging-postman-requests/说明了如何使用Postman检查响应标头。
答案 1 :(得分:0)
我认为问题可能出在此行上:
restTemplate.exchange(url, HttpMethod.GET, entity, Flight[].class);
当httpmethod是GET时,resttemplate会忽略主体,因此不会包含您的实体。将httpmethod更改为POST,然后查看目标服务是否接收到有效负载。如果控制它,则可能必须更改目标服务才能使其接受POST。