如何解决 MismatchedInputException: Cannot construct instance of `java.util.HashSet` 异常
How to solve the MismatchedInputException: Cannot construct instance of `java.util.HashSet` exception
在我的程序中,我试图更新用户的电子邮件。但它给了我一个例外
nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
construct instance of java.util.HashSet
但是当我通过添加技能和电子邮件更新用户时,它并没有给我这个例外。
我试图创建一个默认构造函数,但没有成功。我也尝试过使用 Json creator.It 也没有用 me.I 可以看到问题是当我通过技能集为空时这个异常是 rising.But 我不知道没有明确的想法,如何克服这种情况。
这是我的员工实体
@Entity
@Table(name = "Employee")
public class Employee implements Serializable{
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long emp_id;
@Column(name = "emp_fname")
private String emp_fname;
@Column(name = "emp_lname")
private String emp_lname;
@Column(name = "emp_email")
private String emp_email;
@Column(name = "emp_dob")
private Date emp_dob;
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "emp_skills",
joinColumns = @JoinColumn(name = "emp_id", referencedColumnName = "emp_id"),
inverseJoinColumns = @JoinColumn(name = "s_id",referencedColumnName = "s_id"))
private Set<Skills> skills;
protected Employee(){
}
public Employee(String emp_fname,String emp_lname,String emp_email,Date emp_dob){
this.emp_fname = emp_fname;
this.emp_lname = emp_lname;
this.emp_email = emp_email;
this.emp_dob = emp_dob;
}
public Employee(Long emp_id,String emp_fname,String emp_lname,String emp_email,Date emp_dob,Set<Skills> skills){
this.emp_id = emp_id;
this.emp_fname = emp_fname;
this.emp_lname = emp_lname;
this.emp_email = emp_email;
this.emp_dob = emp_dob;
this.skills = skills;
}
//all the getters and setter
这是我的技能实体
@Entity
@Table(name = "Skills")
public class Skills implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long s_id;
@Column(name = "s_name")
private String s_name;
@ManyToMany(mappedBy = "skills",cascade = CascadeType.ALL)
@JsonIgnore
private Set<Employee> employees;
protected Skills(){
}
public Skills(String s_name){
this.s_name = s_name;
}
//all the getters and setter
这是我的控制器class更新用户的方法
@Autowired
EmployeeRepository repository;
@RequestMapping("/updateEmployee")
@PostMapping("/updateEmployee")
@CrossOrigin
public void updateEmployee(@RequestBody Employee employee, BindingResult bindingResult){
Employee empToBeUpdated = null;
for (Employee emp : repository.findAll()){
if (emp.getEmp_id()==employee.getEmp_id()){
empToBeUpdated = emp;
}
}
if (!employee.getEmp_fname().equals("")&&employee.getEmp_fname()!=null){
empToBeUpdated.setEmp_fname(employee.getEmp_fname());
}
if (!employee.getEmp_lname().equals("")&&employee.getEmp_lname()!=null){
empToBeUpdated.setEmp_lname(employee.getEmp_lname());
}
if (!employee.getEmp_email().equals("")&&employee.getEmp_email()!=null){
empToBeUpdated.setEmp_email(employee.getEmp_email());
}
if (employee.getEmp_dob()!=null){
empToBeUpdated.setEmp_dob(employee.getEmp_dob());
}
if (employee.getSkills()!= null){
empToBeUpdated.setSkills(employee.getSkills());
}
repository.save(empToBeUpdated);
}
这是我遇到的错误
Resolved
[org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot construct instance of java.util.HashSet
(although at least one Creator exists): no String-argument
constructor/factory method to deserialize from String value ('');
nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
construct instance of java.util.HashSet
(although at least one
Creator exists): no String-argument constructor/factory method to
deserialize from String value ('') at [Source: (PushbackInputStream);
line: 1, column: 94] (through reference chain:
com.ems.assignment1.model.Employee["skills"])]
我遇到的问题是,后端需要一个技能集,但是如果当用户没有从技能列表中选择任何技能时,前端传递一个空字符串。
如果用户没有 select 技能列表中的任何技能,我通过从 angular 前端发送一个空数组来解决这个问题。
constructor(private apiService: ApiService) {
this.nullSkills = [];
}
if (this.form.controls.skillsSet.value == null || this.form.controls.skillsSet.value === '') {
this.employee.skills = this.nullSkills;
} else {
this.employee.skills = this.form.controls.skillsSet.value;
}
在我的程序中,我试图更新用户的电子邮件。但它给了我一个例外
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of
java.util.HashSet
但是当我通过添加技能和电子邮件更新用户时,它并没有给我这个例外。
我试图创建一个默认构造函数,但没有成功。我也尝试过使用 Json creator.It 也没有用 me.I 可以看到问题是当我通过技能集为空时这个异常是 rising.But 我不知道没有明确的想法,如何克服这种情况。
这是我的员工实体
@Entity
@Table(name = "Employee")
public class Employee implements Serializable{
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long emp_id;
@Column(name = "emp_fname")
private String emp_fname;
@Column(name = "emp_lname")
private String emp_lname;
@Column(name = "emp_email")
private String emp_email;
@Column(name = "emp_dob")
private Date emp_dob;
@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(name = "emp_skills",
joinColumns = @JoinColumn(name = "emp_id", referencedColumnName = "emp_id"),
inverseJoinColumns = @JoinColumn(name = "s_id",referencedColumnName = "s_id"))
private Set<Skills> skills;
protected Employee(){
}
public Employee(String emp_fname,String emp_lname,String emp_email,Date emp_dob){
this.emp_fname = emp_fname;
this.emp_lname = emp_lname;
this.emp_email = emp_email;
this.emp_dob = emp_dob;
}
public Employee(Long emp_id,String emp_fname,String emp_lname,String emp_email,Date emp_dob,Set<Skills> skills){
this.emp_id = emp_id;
this.emp_fname = emp_fname;
this.emp_lname = emp_lname;
this.emp_email = emp_email;
this.emp_dob = emp_dob;
this.skills = skills;
}
//all the getters and setter
这是我的技能实体
@Entity
@Table(name = "Skills")
public class Skills implements Serializable {
private static final long serialVersionUID = -3009157732242241606L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long s_id;
@Column(name = "s_name")
private String s_name;
@ManyToMany(mappedBy = "skills",cascade = CascadeType.ALL)
@JsonIgnore
private Set<Employee> employees;
protected Skills(){
}
public Skills(String s_name){
this.s_name = s_name;
}
//all the getters and setter
这是我的控制器class更新用户的方法
@Autowired
EmployeeRepository repository;
@RequestMapping("/updateEmployee")
@PostMapping("/updateEmployee")
@CrossOrigin
public void updateEmployee(@RequestBody Employee employee, BindingResult bindingResult){
Employee empToBeUpdated = null;
for (Employee emp : repository.findAll()){
if (emp.getEmp_id()==employee.getEmp_id()){
empToBeUpdated = emp;
}
}
if (!employee.getEmp_fname().equals("")&&employee.getEmp_fname()!=null){
empToBeUpdated.setEmp_fname(employee.getEmp_fname());
}
if (!employee.getEmp_lname().equals("")&&employee.getEmp_lname()!=null){
empToBeUpdated.setEmp_lname(employee.getEmp_lname());
}
if (!employee.getEmp_email().equals("")&&employee.getEmp_email()!=null){
empToBeUpdated.setEmp_email(employee.getEmp_email());
}
if (employee.getEmp_dob()!=null){
empToBeUpdated.setEmp_dob(employee.getEmp_dob());
}
if (employee.getSkills()!= null){
empToBeUpdated.setSkills(employee.getSkills());
}
repository.save(empToBeUpdated);
}
这是我遇到的错误
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
java.util.HashSet
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (''); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofjava.util.HashSet
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('') at [Source: (PushbackInputStream); line: 1, column: 94] (through reference chain: com.ems.assignment1.model.Employee["skills"])]
我遇到的问题是,后端需要一个技能集,但是如果当用户没有从技能列表中选择任何技能时,前端传递一个空字符串。 如果用户没有 select 技能列表中的任何技能,我通过从 angular 前端发送一个空数组来解决这个问题。
constructor(private apiService: ApiService) {
this.nullSkills = [];
}
if (this.form.controls.skillsSet.value == null || this.form.controls.skillsSet.value === '') {
this.employee.skills = this.nullSkills;
} else {
this.employee.skills = this.form.controls.skillsSet.value;
}