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
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @Danix! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Danix
DanixOP7mo ago
{
"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());
}
Peter Rader
Peter Rader7mo ago
As you can see here: Your java-object is a map/graph:
No description
Peter Rader
Peter Rader7mo ago
IssuedBooks and BookDetails reference the same books. Json can not use references. Json is neither a graph nor a map, json is a tree.
Peter Rader
Peter Rader7mo ago
In json the tree looks like this:
No description
Danix
DanixOP7mo ago
So what's the actual mistake of mine right now ? @Peter Rader @dan1st | Daniel Can u check it once
dan1st
dan1st7mo ago
Are there any other getters you have omitted? specifically getIssuedBooks() for example
JavaBot
JavaBot7mo ago
💤 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.
Danix
DanixOP7mo ago
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;
}

}
it is all what i have @dan1st | Daniel
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;
}
}
dan1st
dan1st7mo ago
And this is what you are serializing to JSON?
Danix
DanixOP7mo ago
yehh
dan1st
dan1st7mo ago
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?
Danix
DanixOP7mo ago
yehh i am on 17
dan1st
dan1st7mo ago
then you can use records for that
Danix
DanixOP7mo ago
and when i try to get Students then i got something like this ->
{
"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": []
},
there are two field of booksDetails
dan1st
dan1st7mo ago
The issue here is that you call the field booksDetails but the getter is called getIssuedBooks
Danix
DanixOP7mo ago
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;
}
}
dan1st
dan1st7mo ago
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){}
and
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()
);
}
That's with DTOs YeahYeah you shouldn't get the error any more with that and you can use the same JSON annotations there
Danix
DanixOP7mo ago
Should i have to make a Different DTO class for it
dan1st
dan1st7mo ago
That was my suggestion here These two lines would be your DTO classes
JavaBot
JavaBot7mo ago
💤 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.
Danix
DanixOP7mo ago
nothing fix it @dan1st | Daniel
dan1st
dan1st7mo ago
Can you show the classes you are serializing again? And the controller?
Danix
DanixOP7mo ago
i am getting hibernate query like this when i am hitting getAll Students like this
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=?
with this result
{
"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"
}
]
}
]
dan1st
dan1st7mo ago
Arer you using DTOs? looks like the getter and field names are not exactly compatible
Danix
DanixOP7mo ago
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;
}

}
dan1st
dan1st7mo ago
if you call the field bookDetails and the getter booksDetails, it won't work
Danix
DanixOP7mo ago
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;
}
}
Danix
DanixOP7mo ago
Service class
Danix
DanixOP7mo ago
the last method is getAll
@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);

}
here is the controller @dan1st | Daniel are u here ?
dan1st
dan1st7mo ago
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;
}
literally what I told you
Danix
DanixOP7mo ago
i think this is what i have done right ? .
dan1st
dan1st7mo ago
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 DTOs
Danix
DanixOP7mo ago
okk ok got it btw whats the useCase of DTO and when and why we have to use it ?
dan1st
dan1st7mo ago
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
Danix
DanixOP7mo ago
hm ok thanks
JavaBot
JavaBot7mo ago
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.
Danix
DanixOP7mo ago
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`)]
dan1st
dan1st7mo ago
Can you show the controller you are deserializing with, the relevant data class and the full stack trace?
Danix
DanixOP7mo ago
@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);
}
}
The stack track is limited with that words only
dan1st
dan1st7mo ago
oh and also the JSON you are passing to the endpoint What's your current StudentDetails and the JSON you are passing to that?
Danix
DanixOP7mo ago
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));
}
dan1st
dan1st7mo ago
You still haven't shown me the JSON you are passing to that controller
Danix
DanixOP7mo ago
{
"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"
}
]
}
]
`
dan1st
dan1st7mo ago
Why is there an [] around it? in the first and last line
Danix
DanixOP7mo ago
ohh i literally noticed it 2 sec ago sorry for distrubing you
JavaBot
JavaBot7mo ago
💤 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.

Did you find this page helpful?