javaspringspring-boot

Hierarchical entities data is not returned from a REST API


I have an API method implemented in spring boot for Courses. It fetches the course by topic Id. The Course class is implemented as:

@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;     
    }
        
}

And the API method is implemented as:

@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;         
    }       
}

The method implementation is simple, it tries to get all the courses based on the topic id and return it as a RestMessage Object. I'm using postman for the testing and in the response I am getting the list of Course but the Topic entity data is discarded.

The API response is as:

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

And the RestMessage Class is defined as:

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;
    }
    
}

However, I have tried to debug the API endpoint and before returning the RestMessage object I have data in the required shape but after getting the json response the Topic object is truncated for all the courses.

The debug data image is attached:

enter image description here

I wonder what I am doing wrong in this case?


Solution

  • The field topic from Course doesn't have a getter, that's why is ignored by JSON serializer.