ためrestTemplateを設定:restTemplateを呼び出すときしかし、私はエラー春ブーツ - 私はこのチュートリアルを使用してRESTfulなWebサービスを消費しようとしていたコンテンツ・タイプapplication/jsonの
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]
Iドン次取得 Consuming a RESTful Web Service
content-typeがtext/htmlに設定されている理由を理解できません。 application/json私はjacksonをpom.xmlに依存していますし、spring-bootは私のためにそれを正しく設定する必要があります。
ジャクソンJSON処理ライブラリをクラスパスにあるので、RestTemplateは
はここに私のrestTemplate Beanの入ってくるJSONデータを変換する(メッセージコンバータを介して)それを使用します
@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";
}
}
私が間違っているの何任意のアイデアや方法、コンテンツタイプのアプリケーション/ JSONを使用するためにrestTemplateを教えて?
また、私はこれを試してみましたが、私は応答で、「のContent-Type」ヘッダが設定されていないように見えます
@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";
}
ありがとうございますが、私はすでに高度なレストクライアントを介してリクエストしようとしましたが、私はjsonとxmlで応答を返します。私はまた、サーバー側の著者であり、私は{MediaType.APPLICATION_JSON_VALUE、MediaType.APPLICATION_XML_VALUE}を生成しました。 –
jsonやxmlタイプの応答を生成する場合は、 "Accept:application/json "ヘッダーを使用して、正しい応答形式を取得するようにします。ここでは、その方法の例です。http://stackoverflow.com/questions/19238715/how-to-set-an-accept-header-on-spring-resttemplate-request – Abhi
私はすでにそれを試しました(私の質問を参照してください - 最後のスニペット)しかし、私はこのラインを非難しないでくださいHttpEntityエンティティ=新しいHttpEntity ( "パラメータ"、ヘッダー); 「パラメータ」とは何ですか? –