java - Spring-返回对象列表的请求方法显示错误

标签 java spring spring-boot spring-restcontroller

我正在尝试将对象列表返回到 /data 端点。 以下是其余 Controller 的代码:

@RestController
public class ApiController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "<h1>Welcome to Spring REST API</h1>";
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody List<UserData> data() {
        return new UserService().getData();
    }

}

服务代码:

public class UserService {

    private List<UserData> data;

    public UserService() {
        this.data = new ArrayList<UserData>();
        this.data.add(new UserData(1, "Leanne Graham", "bret", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
                "1-770-736-8031 x56442"));
        this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
                "010-692-6593 x09125"));
        this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
                "ramiro.info", "1-463-123-4447"));
        this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
                "kale.biz", "1-770-736-8037"));
        this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "Keebler LLC",
                "demarco.info", "1-344-736-8031 x564"));
    }

    /**
     * @return the data
     */
    public List<UserData> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<UserData> data) {
        this.data = data;
    }

}

用户数据代码:

public class UserData {

    int id;
    String name, username, email, company, website, phone;

    public UserData(int id, String name, String username, String email, String company, String website, String phone) {
        this.id = id;
        this.name = name;
        this.username = username;
        this.email = email;
        this.company = company;
        this.website = website;
        this.phone = phone;
    }

}

UserData 类用于表示要在列表中传递的各个数据。

抛出以下错误和异常:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.example.springrest.UserData]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.springrest.UserData and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])] with root cause

最佳答案

试试这个

Controller 代码

@RestController
public class ApiController {

    @AutoWired
    UserService userservice;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "<h1>Welcome to Spring REST API</h1>";
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET, produces = produces = MediaType.APPLICATION_JSON_VALUE)
    public List<UserData> data() {
        return new userservice.getData();
    }

}

服务代码

@Service
public class UserService {

    private List<UserData> data;

    public UserService() {
        this.data = new ArrayList<UserData>();
        this.data.add(new UserData(1, "Leanne Graham", "bret", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
                "1-770-736-8031 x56442"));
        this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
                "010-692-6593 x09125"));
        this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
                "ramiro.info", "1-463-123-4447"));
        this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
                "kale.biz", "1-770-736-8037"));
        this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "Keebler LLC",
                "demarco.info", "1-344-736-8031 x564"));
    }

    /**
     * @return the data
     */
    public List<UserData> getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(List<UserData> data) {
        this.data = data;
    }

}

关于java - Spring-返回对象列表的请求方法显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55145559/

相关文章:

java - 使用 Java Config 和 Spring Boot 从数据库加载属性

java - 检测动画结束(Android Studio)

java - java.util.Date 的有效范围?

java - org.springframework.web.client.RestClientException : Could not extract response:

java - 求Java EE书籍推荐

java - hibernate 条件列表后的 Postgresql 无限循环

java - 在 MAPPER XML 中的 SELECT 语句中传递多个参数

java - 在 grpc-java 中有支持故障转移的官方方法吗?

spring-boot - 我的 spring-boot mvc 应用程序总是通过显示关闭

spring-boot - 执行完第一个作业后运行第二个批处理作业 Spring boot