[英]Spring Boot JSON Many to one
I have problem with my code... 我的代码有问题...
I create my single page application with Spring Boot and Angularjs. 我使用Spring Boot和Angularjs创建单页应用程序。
Here is my users class: 这是我的用户类:
@Entity
@Table(name = "admins")
public class Admin {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer adminid;
private String name;
private String surname;
private String email;
private String login;
private String password;
@ManyToOne
@JoinColumn(name = "groupid")
private Group groupid;
private Boolean active;
@Temporal(TemporalType.TIMESTAMP)
private Date modifydate;
@Temporal(TemporalType.TIMESTAMP)
private Date createdate;
public Admin() {
}
//setters ang getters
}
And my secend entity class looks 我的第二实体类看起来
@Entity
@Table(name = "groups")
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer groupid;
private String groupname;
@Temporal(TemporalType.TIMESTAMP)
private Date modifydate;
@Temporal(TemporalType.TIMESTAMP)
private Date createdate;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "groupid")
@JsonIgnore
private List<Admin> groupusers;
public Group() {
}
public Group(Integer groupid) {
this.groupid = groupid;
}
}
My frontend application send request to the server, like this 我的前端应用程序将请求发送到服务器,像这样
{adminid: null, name: "dsa", surname: "dsa", email: "d", login: "sada", groupid: "1", active: true}
I have problem with groupid property when i try add new admin... 尝试添加新管理员时,groupid属性出现问题。
WARN 27742 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.patryk.model.Group: no String-argument constructor/factory method to deserialize from String value ('1')
at [Source: java.io.PushbackInputStream@9fe378; line: 1, column: 83] (through reference chain: com.patryk.model.Admin["groupid"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.patryk.model.Group: no String-argument constructor/factory method to deserialize from String value ('1')
at [Source: java.io.PushbackInputStream@9fe378; line: 1, column: 83] (through reference chain: com.patryk.model.Admin["groupid"])
My controller: 我的控制器:
@RestController
@RequestMapping("/api/admins")
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> addUser(@RequestBody Admin admin)
{
adminService.save(admin);
return new ResponseEntity<Admin> (admin,HttpStatus.OK);
}
}
How can I find a solution? 我如何找到解决方案? Anyone know what i am doing wrong ? 有人知道我在做什么错吗?
You have to construct the object from the groupid
. 您必须从groupid
构造对象。
{
adminid: null,
name: "dsa",
surname: "dsa",
email: "d",
login: "sada",
groupid: { "groupid": "1"},
active: true
}
If you want to retain your original json that isn't working, you'll need to create a custom Deserializer so that you can convert "groupid": "1"
into the Group
entity. 如果要保留无法使用的原始json,则需要创建一个自定义反序列化器,以便可以将"groupid": "1"
转换为Group
实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.