Reputation: 11
I have a class called User and Department which point to respective tables in the mysql database(Many-to-many relationship). I want to create a custom deserializer for the List departments in the User class.
my POST Json looks like this:
{ "userId": 1456, "firstName": "David", "middleName": "", "lastName": "Bekham", "gender": "M", "rollNo": "", "semester": 0, "section": "", "email": "[email protected]", "password": "d123avid", "phone": "9844547852", "status": 0, "userType": 1, "userBio": "i teach statistics", "profilePicture": null, "dob": "1999-03-21", "facultyType": 1, "departments": [109] }
My User class definition is as follows:
@Entity
@Table(name="user")
public class User {
@Id
@Column(name="user_id")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private long userId;
@Column(name="first_name")
private String firstName;
@Column(name="middle_name")
private String middleName;
@Column(name="last_name")
private String lastName;
@Column(name="gender")
private char gender;
@Column(name="roll")
private String rollNo;
@Column(name="semester")
private byte semester;
@Column(name="section")
private char section;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="phone")
private String phone;
@Column(name="status")
private byte status;
@Column(name="user_type")
private byte userType;
@Column(name="user_bio")
private String userBio;
@Column(name="profile_picture")
private String profilePicture;
@Column(name="dob")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate dob;
@Column(name="faculty_type")// 0 is for student, 1 for permanent and 2 for visiting
private byte facultyType;
//for join table
@ManyToMany(fetch=FetchType.EAGER,
cascade= {CascadeType.PERSIST, CascadeType.MERGE,CascadeType.DETACH, CascadeType.REFRESH})
@JoinTable(
name="user_department",
joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns=@JoinColumn(name="dept_id")
)
private List<Department> departments;
currently i get an error saying that
JSON parse error: Can not construct instance of com.maverick.project.entity.Department: no int/Int-argument constructor/factory method to deserialize from Number value (109); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.maverick.project.entity.Department: no int/Int-argument constructor/factory method to deserialize from Number value (109)
My Department class definition is as follows:
@Entity
@Table(name="department")
public class Department {
@Id
@Column(name="dept_id")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private int deptId;
@Column(name="dept_name")
private String deptName;
@ManyToMany(fetch=FetchType.EAGER,
cascade= {CascadeType.PERSIST, CascadeType.MERGE,CascadeType.DETACH, CascadeType.REFRESH})
@JoinTable(
name="user_department",
joinColumns=@JoinColumn(name="dept_id"),
inverseJoinColumns=@JoinColumn(name="user_id")
)
@JsonIgnore
private List<User> users;
Upvotes: 0
Views: 297
Reputation: 38645
You do not need custom deserialiser. You can add one argument constructor which takes int
:
public Department() {
}
public Department(int deptId) {
this.deptId = deptId;
}
And it will be automatically deserialised to Department
. After deserialisation you need to do extra configuration if it is needed.
Upvotes: 0