我有一个本地托管的网络服务。 Controller 在返回单个字符串值时工作正常。但每当我尝试返回列表或特定对象时,它都会返回 Whitelabel 错误页面,错误代码为 500。下面是我尝试返回列表时的错误。
出现意外错误(类型=内部服务器错误,状态=500)。 找不到类型返回值的转换器:class java.util.Arrays$ArrayList
当我尝试访问特定对象(例如/topic/1)时,出现以下错误。 出现意外错误(类型=内部服务器错误,状态=500)。 找不到类型返回值的转换器:class com.spring.rest.topic.Topic
注意:1.我看到 jackson 依赖项是由 spring-boot-starter-web 依赖项添加的。所以不需要在pom.xml中再次添加。 2.我的 Pojo 类 Topic 具有所有属性的公共(public) getter。 以下是我的类(class):
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>
最佳答案
您需要在依赖树中包含一个 json 映射器,例如 com.fasterxml.jackson.core 的 jackson-databind。即
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
关于java - 尝试使用 Spring Boot 调用本地 Web 服务时返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443226/