JSON RESPONSE ERROR
I dont know why whenever i am adding data to my RestApi i am getting double json response of book class here is the response ->
47 Replies
⌛
This post has been reserved for your question.
Hey @Danix! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
{
"roll_no": 40,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
}
],
"issuedBooks": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
}
]
}
{
"roll_no": 40,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
}
],
"issuedBooks": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-26T17:00:09.5337977",
"returnDate": "2024-07-07T10:00:00"
}
]
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@PostMapping("/addStudent")
public ResponseEntity<StudentDetails> addUser(@RequestBody StudentDetails studentDetails) {
Optional<StudentDetails> optional = detailsService.saveStudentDetails(studentDetails);
return optional.map(savedStudent -> ResponseEntity.status(HttpStatus.CREATED).body(savedStudent))
.orElse(ResponseEntity.internalServerError().build());
}
@PostMapping("/addStudent")
public ResponseEntity<StudentDetails> addUser(@RequestBody StudentDetails studentDetails) {
Optional<StudentDetails> optional = detailsService.saveStudentDetails(studentDetails);
return optional.map(savedStudent -> ResponseEntity.status(HttpStatus.CREATED).body(savedStudent))
.orElse(ResponseEntity.internalServerError().build());
}
As you can see here: Your java-object is a map/graph:
IssuedBooks and BookDetails reference the same books. Json can not use references. Json is neither a graph nor a map, json is a tree.
In json the tree looks like this:
So what's the actual mistake of mine right now ? @Peter Rader
@dan1st | Daniel Can u check it once
Are there any other getters you have omitted?
specifically
getIssuedBooks()
for example💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getIssuedBooks() {
return booksDetails;
}
public void setIssuedBooks(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getIssuedBooks() {
return booksDetails;
}
public void setIssuedBooks(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
And this is what you are serializing to JSON?
yehh
I think it's probably best to convert it to a DTO before
e.g. using records
I assume you are on Java 17 or later?
yehh i am on 17
then you can use records for that
and when i try to get Students then i got something like this ->
there are two field of booksDetails
{
"roll_no": 40,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [],
"bookDetails": []
},
{
"roll_no": 40,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [],
"bookDetails": []
},
The issue here is that you call the field
booksDetails
but the getter is called getIssuedBooks
no now i have changed it
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
public record StudentDetailsDTO(Long rollNo, String name, String email, String phoneNumber, List<BooksDetailsDTO> bookDetails){}
public record BooksDetailsDTO(Long bookId, String bookName, LocalDateTime issueDate, LocalDateTime returnDate){}
public record StudentDetailsDTO(Long rollNo, String name, String email, String phoneNumber, List<BooksDetailsDTO> bookDetails){}
public record BooksDetailsDTO(Long bookId, String bookName, LocalDateTime issueDate, LocalDateTime returnDate){}
private StudentDetailsDTO toDTO(StudentDetails details){
return new StudentDetailsDTO(details.getRoll_no(), details.getName(), details.getEmail(), details.getPhoneNumber(),
details.getBookDetails().stream(bookDetail -> new BooksDetailsDTO(bookDetail.getBookId(), bookDetail.getBookName(), bookDetail.getIssueDate(), bookDetail.getReturnDate())).toList()
);
}
private StudentDetailsDTO toDTO(StudentDetails details){
return new StudentDetailsDTO(details.getRoll_no(), details.getName(), details.getEmail(), details.getPhoneNumber(),
details.getBookDetails().stream(bookDetail -> new BooksDetailsDTO(bookDetail.getBookId(), bookDetail.getBookName(), bookDetail.getIssueDate(), bookDetail.getReturnDate())).toList()
);
}
Should i have to make a Different DTO class for it
That was my suggestion here
These two lines would be your DTO classes
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
nothing fix it @dan1st | Daniel
Can you show the classes you are serializing again?
And the controller?
i am getting hibernate query like this when i am hitting getAll Students like this
with this result
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from user_info sd1_0
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.book_name,bd1_0.issue_date,bd1_0.return_date from issued_books bd1_0 where bd1_0.user_roll_no=?
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from user_info sd1_0
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.book_name,bd1_0.issue_date,bd1_0.return_date from issued_books bd1_0 where bd1_0.user_roll_no=?
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from user_info sd1_0
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.book_name,bd1_0.issue_date,bd1_0.return_date from issued_books bd1_0 where bd1_0.user_roll_no=?
Hibernate: select sd1_0.roll_no,sd1_0.email,sd1_0.name,sd1_0.phone_number from user_info sd1_0
Hibernate: select bd1_0.user_roll_no,bd1_0.book_id,bd1_0.book_name,bd1_0.issue_date,bd1_0.return_date from issued_books bd1_0 where bd1_0.user_roll_no=?
{
"roll_no": 79,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
}
],
"bookDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
}
]
}
]
{
"roll_no": 79,
"name": "Danix",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
}
],
"bookDetails": [
{
"bookId": 103,
"bookName": "Clean Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "Java Code",
"issueDate": "2024-06-27T16:33:54.177907",
"returnDate": "2024-07-07T10:00:00"
}
]
}
]
Arer you using DTOs?
looks like the getter and field names are not exactly compatible
i was using them but nothig changes so i retrun to the last way
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
if you call the field bookDetails and the getter booksDetails, it won't work
student class
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.time.LocalDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "Issued_Books")
public class BooksDetails {
@Id
private Long bookId;
private String bookName;
@DateTimeFormat
private LocalDateTime issueDate;
@DateTimeFormat
private LocalDateTime returnDate;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "user_roll_no")
private StudentDetails user;
public BooksDetails() {
}
public BooksDetails(StudentDetails user, Long bookId , String bookName) {
this.user = user;
this.bookId = bookId;
this.bookName = bookName;
}
public StudentDetails getUser() {
return user;
}
public void setUser(StudentDetails user) {
this.user = user;
}
public Long getBookId() {
return bookId;
}
public void setBookId(Long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public LocalDateTime getIssueDate() {
return issueDate;
}
public void setIssueDate(LocalDateTime issueDate) {
this.issueDate = issueDate;
}
public LocalDateTime getReturnDate() {
return returnDate;
}
public void setReturnDate(LocalDateTime returnDate) {
this.returnDate = returnDate;
}
}
Service class
the last method is getAll
here is the controller
@dan1st | Daniel are u here ?
@GetMapping("/getAllBooks")
public ResponseEntity<List<BooksDetails>> getAllBooks() {
Iterable<BooksDetails> allBooks = detailsService.getAllBooks();
List<BooksDetails> allBooksList = new ArrayList<BooksDetails>();
allBooks.forEach(allBooksList::add);
if (allBooksList.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(allBooksList, HttpStatus.OK);
}
@GetMapping("/getAllBooks")
public ResponseEntity<List<BooksDetails>> getAllBooks() {
Iterable<BooksDetails> allBooks = detailsService.getAllBooks();
List<BooksDetails> allBooksList = new ArrayList<BooksDetails>();
allBooks.forEach(allBooksList::add);
if (allBooksList.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(allBooksList, HttpStatus.OK);
}
private List<BooksDetails> booksDetails;
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
private List<BooksDetails> booksDetails;
public List<BooksDetails> getBookDetails() {
return booksDetails;
}
public void setBookDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
i think this is what i have done right ?
.
booksDetails
has an s
in the middle
getBookDetails
doesn't
If there is a mismatch, you get it twice in the JSON
or just use records as DTOsokk ok got it btw whats the useCase of DTO and when and why we have to use it ?
You have a different class for your entity and for what you serialize as JSON
This gives you more control over what you want your JSON to look like
hm ok thanks
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
this is now i am getting while adding a student
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `com.ShelfSpace.ShelfSpace.rest.model.StudentDetails` from Array value (token `JsonToken.START_ARRAY`)]
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `com.ShelfSpace.ShelfSpace.rest.model.StudentDetails` from Array value (token `JsonToken.START_ARRAY`)]
Can you show the controller you are deserializing with, the relevant data class and the full stack trace?
@PostMapping("/addStudent")
public ResponseEntity<StudentDetails> addUser(@RequestBody StudentDetails studentDetails) {
Optional<StudentDetails> optional = detailsService.saveStudentDetails(studentDetails);
if (optional.isPresent()) {
return new ResponseEntity<>(optional.get(), HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/addStudent")
public ResponseEntity<StudentDetails> addUser(@RequestBody StudentDetails studentDetails) {
Optional<StudentDetails> optional = detailsService.saveStudentDetails(studentDetails);
if (optional.isPresent()) {
return new ResponseEntity<>(optional.get(), HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
oh and also the JSON you are passing to the endpoint
What's your current
StudentDetails
and the JSON you are passing to that?package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBooksDetails() {
return booksDetails;
}
public void setBooksDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
package com.ShelfSpace.ShelfSpace.rest.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
@Entity
@Table(name = "User_Info")
public class StudentDetails {
@Id
private Long roll_no;
private String name;
private String email;
private String phoneNumber;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<BooksDetails> booksDetails;
public StudentDetails() {}
public StudentDetails(Long roll_no, String name, String email, String phoneNumber, List<BooksDetails> booksDetails) {
this.roll_no = roll_no;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.booksDetails = booksDetails;
}
public Long getRoll_no() {
return roll_no;
}
public void setRoll_no(Long roll_no) {
this.roll_no = roll_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public List<BooksDetails> getBooksDetails() {
return booksDetails;
}
public void setBooksDetails(List<BooksDetails> booksDetails) {
this.booksDetails = booksDetails;
}
}
@Override
public Optional<StudentDetails> saveStudentDetails(StudentDetails details) {
for (BooksDetails booksDetails : details.getBooksDetails()) {
booksDetails.setIssueDate(LocalDateTime.now());
booksDetails.setUser(details);
}
return Optional.ofNullable(detailsRepository.save(details));
}
@Override
public Optional<StudentDetails> saveStudentDetails(StudentDetails details) {
for (BooksDetails booksDetails : details.getBooksDetails()) {
booksDetails.setIssueDate(LocalDateTime.now());
booksDetails.setUser(details);
}
return Optional.ofNullable(detailsRepository.save(details));
}
You still haven't shown me the JSON you are passing to that controller
{
"roll_no": 49,
"name": "Deepak",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Be a React God ",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "js Fraework",
"returnDate": "2024-07-07T10:00:00"
}
]
}
]
{
"roll_no": 49,
"name": "Deepak",
"email": "Danix#[email protected]",
"phoneNumber": "123213321",
"booksDetails": [
{
"bookId": 103,
"bookName": "Be a React God ",
"returnDate": "2024-07-07T10:00:00"
},
{
"bookId": 290,
"bookName": "js Fraework",
"returnDate": "2024-07-07T10:00:00"
}
]
}
]
Why is there an
[]
around it?
in the first and last lineohh i literally noticed it 2 sec ago sorry for distrubing you
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.