尝试使用springboot调用本地Web服务时返回错误

时间:2017-04-16 23:26:21

标签: java rest spring-boot

我有一个本地托管的网络服务。控制器返回单个String值时工作正常。但每当我尝试返回列表或特定的对象时,它返回Whitelabel错误页面,错误代码为500.以下是我尝试返回列表时的错误。

出现意外错误(type = Internal Server Error,status = 500)。 找不到类型的返回值的转换器:class java.util.Arrays $ ArrayList

当我尝试访问特定对象时,我得到以下错误,比如/ topic / 1。 出现意外错误(type = Internal Server Error,status = 500)。 找不到类型为:class com.spring.rest.topic.Topic

的返回值的转换器

注意:1。我看到spring-boot-starter-web依赖项添加了jackson依赖项。所以不需要再次在pom.xml中添加。 2.我的Pojo课程,主题,有所有属性的公共getter。 以下是我的课程:

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>

1 个答案:

答案 0 :(得分:1)

您需要在依赖关系树中加入json映射器,例如com.fasterxml.jackson.core&#39; s jackson-databind。即。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.8</version>
</dependency>