2017-03-06 1 views
0

내 코드에 문제가있다 ...봄 부팅 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 
    } 

그리고 내 secend 엔티티 클래스는 내가 가진이

{adminid: null, name: "dsa", surname: "dsa", email: "d", login: "sada", groupid: "1", active: true} 

처럼

@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; 
} 
} 

내 프론트 엔드 응용 프로그램이 서버에 요청을 보낼 같습니다 여기

내 사용자 클래스입니다 새 관리자를 추가하려고하면 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: [email protected]; 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: [email protected]; 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); 
} 
} 

가 어떻게 해결책을 찾을 수 있습니까? 누구든지 내가 뭘 잘못하고 있는지 알아?

답변

0

groupid에서 개체를 구성해야합니다. 당신이 작동하지 않는 원래 JSON을 유지하려면 당신이 Group 엔티티에 "groupid": "1"을 변환 할 수 있도록

{ 
    adminid: null, 
    name: "dsa", 
    surname: "dsa", 
    email: "d", 
    login: "sada", 
    groupid: { "groupid": "1"}, 
    active: true 
} 

, 당신은 사용자 정의 디시리얼라이저를 만들어야합니다.