[英]Spring Boot JSON Many to one
我的代码有问题...
我使用Spring Boot和Angularjs创建单页应用程序。
这是我的用户类:
@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
}
我的第二实体类看起来
@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;
}
}
我的前端应用程序将请求发送到服务器,像这样
{adminid: null, name: "dsa", surname: "dsa", email: "d", login: "sada", groupid: "1", active: true}
尝试添加新管理员时,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"])
我的控制器:
@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);
}
}
我如何找到解决方案? 有人知道我在做什么错吗?
您必须从groupid
构造对象。
{
adminid: null,
name: "dsa",
surname: "dsa",
email: "d",
login: "sada",
groupid: { "groupid": "1"},
active: true
}
如果要保留无法使用的原始json,则需要创建一个自定义反序列化器,以便可以将"groupid": "1"
转换为Group
实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.