我无法将UI中的JSON对象发布到Spring Boot控制器。
这是我的控制器,我已注释掉控制器代码,只是返回一个新对象。似乎JSON没有正确解析为域对象:
PersonnelController:
@Controller
@RequestMapping(value = "/Personnel")
public class PersonnelController {
@RequestMapping(value = "update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Personnel updateJson(@RequestBody Personnel personnel) {
System.out.println("update personnel json : " + personnel.toString());
return new Personnel();
}
}
我的控制器不是REST控制器,并且我正在使用内容协商来能够一起使用Spring模型和REST API。
这些是我的课程:
用户:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class User extends AbstractDomainClass {
@Column(unique = true)
private String username;
@Transient
private String password;
private String encryptedPassword;
private boolean enabled = true;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable
@JsonBackReference(value = "roles")
private List<Role> roles = new ArrayList<>();
}
人员:
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Personnel extends User {
private String firstName;
private String lastName;
private long nationalId;
private long perssonnelId;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;
@ManyToOne(fetch = FetchType.LAZY)
private Company company;
}
公司:
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Company extends AbstractDomainClass {
@Column(unique = true)
private String name;
private String description;
private String address;
private String adminName;
@OneToMany(fetch = FetchType.LAZY , cascade = CascadeType.PERSIST , orphanRemoval = true)
@JoinTable
@JsonBackReference
private List<Personnel> personnels = new ArrayList<>();
@OneToOne(fetch = FetchType.EAGER , cascade = CascadeType.ALL)
private User admin;
}
角色:
@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Role extends AbstractDomainClass {
@Column(unique = true)
private String roleName;
@ManyToMany(fetch = FetchType.EAGER , mappedBy = "roles")
private List<User> users = new ArrayList<>();
@ManyToMany
@Fetch(FetchMode.SELECT)
private List<Permission> permissions = new ArrayList<>();
}
(我省略了getter和setters。)
这是我要发布到控制器的JSON:
这是Spring Boot错误:
WARN 10289 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `navid.usermanagementsys.domain.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":26,"version":0,"dateCreated":"2018-08-08T16:02:15.000+0000","name":"company1","description":"Description Description Description Description","address":"IRAN","adminName":"Company admin","admin":{"id":25,"version":0,"dateCreated":"2018-08-08T16:02:15.000+0000","username":"company","password":null,"encryptedPassword":"$2a$10$kw5T5CCep.WwC5kj7Szgyu6akB7Zbx.jgqIz5dQtAx6Oz4F8ywIWq","enabled":true,"dateLastUpdated":"2018-08-08T16:02:15.000+0000"},"dateLastUpdated":"2018-08-08T16:02:15.000+0000"}');
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `navid.usermanagementsys.domain.Company` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value** ('{"id":26,"version":0,"dateCreated":"2018-08-08T16:02:15.000+0000","name":"company1","description":"Description Description Description Description","address":"IRAN","adminName":"Company admin","admin":{"id":25,"version":0,"dateCreated":"2018-08-08T16:02:15.000+0000","username":"company","password":null,"encryptedPassword":"$2a$10$kw5T5CCep.WwC5kj7Szgyu6akB7Zbx.jgqIz5dQtAx6Oz4F8ywIWq","enabled":true,"dateLastUpdated":"2018-08-08T16:02:15.000+0000"},"dateLastUpdated":"2018-08-08T16:02:15.000+0000"}')
at [Source: (PushbackInputStream); line: 1, column: 152] (through reference chain: navid.usermanagementsys.domain.Personnel["company"])
我花了数小时寻找解决方案,并且尝试创建没有args的构造函数,但没有任何解决方案可以解决我的问题。