Spring Boot-层次实体数据未从Rest API返回

时间:2018-12-08 18:01:32

标签: java spring spring-boot

我有一个在Spring Boot中为Courses实现的API方法。它通过主题ID获取课程。课程类的实现方式为:

@Entity
public class Course {

    @Id
    private String id;
    private String name;
    private String description;
    @ManyToOne
    private Topic topic;
    public Course() {

    }

    public Course(String id, String name, String description, String topicId) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
        this.topic = new Topic(topicId, "", "");
    }

    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;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    public void setTopic(Topic t) {     
        this.topic = t;     
    }

}

API方法实现为:

@RequestMapping(method=RequestMethod.GET, value="/topics/{topicId}/courses")
    public RestMessage getAllCourses(@PathVariable String topicId) {                
        try {

            List<Course> course = courseService.getAllCourses(topicId);     
            message = new RestMessage(course,StatusCodeEnum.OK);        
            return message;

        } catch (Exception e) {

            message = new RestMessage(e.getMessage(),StatusCodeEnum.INTERNAL_SERVER_ERROR);
            message.setException(e);

            return message;         
        }       
    }

方法实现很简单,它尝试根据主题id获取所有课程并将其作为RestMessage对象返回。我使用邮递员进行测试,并在响应中得到了“课程”列表,但“主题”实体数据已被丢弃。

api响应如下:

{
    "data": [
        {
            "id": "java-streams",
            "name": "Java Streams",
            "description": "Java Stream learning"
        }
    ],
    "httpStatus": "OK",
    "statusCode": 200,
    "exception": null
}

RestMessage类定义为:

public class RestMessage {

    private Object data;
    private StatusCodeEnum httpStatus;
    private int statusCode;
    private Exception ex;

    public RestMessage() {

    }

    public RestMessage(Object d, StatusCodeEnum c) {
        data = d;
        httpStatus = c;
        statusCode = c.val();
    }

    public void setData(Object d) {
        data =d;
    }

    public void setHttpStatus(StatusCodeEnum c) {
        httpStatus = c;
    }

    public void setStatusCode(int c) {
        statusCode = c;
    }

    public void setException(Exception e) {
        ex = e;
    }

    public void setStatusCode(StatusCodeEnum c) {
        httpStatus = c;
    }

    public Object getData() {
        return data;
    }

    public StatusCodeEnum getHttpStatus() {
        return httpStatus;
    }

    public Exception getException() {
        return ex;
    }

    public int getStatusCode() {
        return statusCode;
    }

}

但是,我尝试调试API端点,并且在返回RestMessage对象之前,我具有所需形状的数据,但是在获取json响应后,所有课程的Topic对象都被截断了。

已附加调试数据映像:

enter image description here

我想知道在这种情况下我做错了什么吗?

2 个答案:

答案 0 :(得分:2)

topic中的字段Course没有吸气剂,这就是为什么JSON序列化程序将其忽略。

答案 1 :(得分:-1)

您可以使用Lombok批注自动生成getter和setter。

只需在类定义上添加@Data,您将生成getter和setter,即不带参数的构造函数。您可以说的所有内容都是 POJO 类。

检查此链接:Project lombok