I am trying to create a handler method in controller class in spring boot to handle post request coming with json in body. I am trying to deserialize this incoming json to object of a class using @RequestBody attribute in spring boot and then trying to save it in my database. I have used @manytomany bidirectional relationship between Account and Customer class. Account.java
@Entity
@Table(name = "Account")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int accountNumber;
public String accountType;
public int balance;
@ManyToMany(cascade = CascadeType.ALL)
@JsonManagedReference
private List<Customer> customers;
}
//skipped setters and getters and constructor
Customer.java
@Entity
@Table(name = "Customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int customerId;
public String firstName;
public String lastName;
public String email;
@ManyToMany(mappedBy = "customers")
@JsonBackReference
private List<Account> accounts;
//skipped constructors , getters and setters.
}
Handler method in controller class
// post method to add record in account table
@PostMapping("/accounts")
public ResponseEntity<Account> postAccount(@RequestBody Account account) {
System.out.println(account);//trying to print incoming account class object
Account ac = this.accountServices.addAccount(account);
return new ResponseEntity<>(ac, HttpStatus.OK);
}
Error on console is
I have started !!!!
2021-08-03 22:52:37.011 INFO 17508 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-08-03 22:52:37.012 INFO 17508 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-08-03 22:52:37.015 INFO 17508 --- [nio-8081-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
2021-08-03 22:52:37.021 WARN 17508 --- [nio-8081-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class ma20099449.foundation.bank.ma20099449_bank.Entities.Account]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (`java.util.List<ma20099449.foundation.bank.ma20099449_bank.Entities.Account>`) not compatible with managed type (ma20099449.foundation.bank.ma20099449_bank.Entities.Account)
2021-08-03 22:52:37.029 WARN 17508 --- [nio-8081-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class ma20099449.foundation.bank.ma20099449_bank.Entities.Account]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'defaultReference': back reference type (`java.util.List<ma20099449.foundation.bank.ma20099449_bank.Entities.Account>`) not compatible with managed type (ma20099449.foundation.bank.ma20099449_bank.Entities.Account)
2021-08-03 22:52:37.031 WARN 17508 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
There is some problem for deserializing the incoming json to Account class object but I don't know the error. Someone please help me with code.