I have problem with my code...
I create my single page application with Spring Boot and 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...
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
.
{
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.