Post object inside another object to mongoDB via Spring rest api

Viewed 462

I'm creating a basic crud application for members in a library. I've created a spring api to get the values and insert it into the DB. So when I check it using postman a error is shown. (500: Internal server error)
Below diagram shows a request i tried to sent.
enter image description here

As shown above I'm sending another object inside the Member object and once I removed the latestBook, there is no error the value is posted.

I want to resolve this issue, please help me with this.

API

@PostMapping("/m")
public Member save(@RequestBody Member member){
    repository.save(member);
    return member;
}

Member.java

@Document(collection = "Members")
public class Member {

@Id
private String id;
private String name;
private Book latestBook;
private String gender;
private int contact;

public Member() {
}

public Member(String id, String name, Book latestBook, String gender, int contact) {
    this.id= id;
    this.name = name;
    this.startMembershipDate = startMembershipDate;
    this.gender = gender;
    this.contactNum = contactNum;
}

 //getters , setters and toString method

}

Book.java

public class Book{

private String name;
private String author;
private int year;

public Book(String name, String author, int year) {
    this.name= name;
    this.author= author;
    this.year = year;
}
    //getters , setters and toString method
}

I think the problem is with the Book class so please help me to solve this issue.
Not only for posting it does not work for delete either.

1 Answers

Solved the issue.

@Document(collection = "Members")
public class Member {

@Id
private String id;
private String name;
private Book latestBook = new Book();
private String gender;
private int contact;

public Member() {
}

public Member(String id, String name, Book latestBook, String gender, int contact) {
    this.id= id;
    this.name = name;
    this.startMembershipDate = startMembershipDate;
    this.gender = gender;
    this.contactNum = contactNum;
}

 //getters , setters and toString method

}

////////////////////////

public class Book{

private String name;
private String author;
private int year;

Book(){}

public Book(String name, String author, int year) {
    this.name= name;
    this.author= author;
    this.year = year;
}
    //getters , setters and toString method
}
Related