私はローカルホストされたWebサービスを持っています。コントローラは、単一のString値を返すときに正常に動作します。しかし、リストまたは特定のオブジェクトのいずれかを返そうとするたびに、エラーコード500のWhitelabelエラーページが返されます。リストを返そうとするとエラーが発生します。springbootを使用してローカルWebサービスにコールしようとしたときにエラーが返される
予期しないエラーが発生しました(タイプ=内部サーバーエラー、ステータス= 500)。 タイプの戻り値のコンバーターが見つかりません:クラスjava.util.Arrays $ ArrayList
特定のオブジェクト、たとえば/ topic/1にアクセスしようとすると、以下のエラーが発生します。 予期しないエラーが発生しました(type = Internal Server Error、status = 500)。 タイプの戻り値のコンバーターが見つかりません:クラスcom.spring.rest.topic.Topic
注:1. jackson依存関係は、spring-boot-starter-web依存関係によって追加されています。したがって、pom.xmlに再度追加する必要はありません。 2.私のPojoクラスTopicは、すべての属性に対してパブリックゲッターを持っています。
Topic.java(POJOクラス):
package com.spring.rest.topic;
public class Topic {
private String id;
private String name;
private String description;
public Topic(){
}
public Topic(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
TopicController.java(Controllerクラス):
package com.spring.rest.topic;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TopicController {
@Autowired //this annotation tells spring to inject an object for the dependency.
private TopicService service;
@RequestMapping("/topics") //spring mvc casts the list to json.
public List<Topic> getAllTopics(){
return service.getAllTopics();
}
@RequestMapping("/topic/{id}")
public Topic getTopic(@PathVariable String id){
return service.getTopic(id);
}
}
TopicService.java(サービスクラス):
は、以下の私のクラスでありますpackage com.spring.rest.topic;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;
//in spring, a service class is of singleton scope.
@Service
public class TopicService {
private List<Topic> topics = Arrays.asList(new Topic("1","SpringMVC","Using spring mvc"),
new Topic("2","RESTful API","Using REST top create a micro services"));
public List<Topic> getAllTopics(){
return topics;
}
public Topic getTopic(String id){
/*Topic topic = new Topic();*/
for(Topic topic : getAllTopics()){
if(topic.getId().equals(id)){
return topic;
}
}
return null;
}
}
pom.xml:
<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>spring.java.rest.demo</groupId>
<artifactId>SpringRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java-version>1.8</java-version>
</properties>
</project>
依存関係を追加した後に機能します。しかし、私はチェックして、同じ依存関係がすでに org.springframework.boot によって追加されていることを確認できました。これはpom.xmlにあります。バージョンの変更はトリックでしたか? –
はい、バージョンはトリックでした! :(しかし、そのような問題を解決するのは難しい –