Skip to content
Advertisement

Nested JSON in JSON

How can I form JSON like that:

{
         "error": false,
         "errorCode": 0,
         "message": [{
             "id": "93",
             "venueName": "Sushi Kuni",
             "venueAddress": "10211 S De Anza Blvd Cupertino CA",
             "showDate": "1531022400",
             "showTime": "",
             "description": ""
         }, {
             "id": "38",
             "venueName": "Two Keys Tavern",
             "venueAddress": "333 S Limestone Lexington KY",
             "showDate": "1531368000",
             "showTime": "8 pm - 1 am",
             "description": ""
         }]
     }

I tried creating models with one-to-one, many-to-one relationships.

@Entity
@Table(name = "tutorials")
public class Tutorial {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tutorial_generator")
    private int id;
    @Column(name = "title")
    private String title;
    @Column(name = "description")
    private String description;
    @Column(name = "published")
    private boolean published;

@Entity
@Table(name = "comments")
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "comment_generator")
    private int id;
    @Lob
    private String content;
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "tutorial_id", nullable = false)
    @JsonIgnore
    private Tutorial tutorial;

Controller:

public ResponseEntity<List<Comment>> getAllCommentsByTutorialId(@PathVariable(value = "tutorialId") int tutorialId) {
        if (!tutorialRepository.existsById(tutorialId)) {
            throw new ResourceNotFoundException("Not found Tutorial with id = " + tutorialId);
        }
        List<Comment> comments = commentRepository.findByTutorialId(tutorialId);
        return new ResponseEntity<>(comments, HttpStatus.OK);
    }

But I got

[
    {
        "id": 1,
        "content": "asdaasfaf"
    },
    {
        "id": 3,
        "content": "ssaduy7tjyjt"
    },
    {
        "id": 4,
        "content": null
    }
]

I read about creating nodes, but didn’t understand, how integrate it with spring data. What i need to nest table Comments in table Tutorials?

Advertisement

Answer

Looks to me as if you are pretty close to the desired outcome. First of all you should add a member to your Tutorial entity:

@OneToMany(mappedBy = "tutorial_id")
private List<Comment> comments;

And the final missing part is making your controller return a tutorial instance:

public ResponseEntity<Tutorial> getTutorialWithComments(@PathVariable(value = "tutorialId") int tutorialId) {
  Tutorial tutorial = tutorialRepository.findById(tutorialId)
    .orElseThrows(() -> new ResourceNotFoundException("Not found Tutorial with id = " + tutorialId));
  return new ResponseEntity<>(tutorial, HttpStatus.OK);
}

I wrote this without any IDE assistance, so please excuse any mistakes. The idea should be clear though, you need to return a tutorial that contains a list of comments.

Advertisement