2017-03-05 40 views
1

我都面臨着轉換器無法處理JSON對象的問題。傑克遜未能反序列化對象一對多

我在數據庫中有兩個對象。一對多的關係。

我有一個AutoService有很多服務。

而且wnen我試圖用郵差到我的服務器發送JSON對象 - 我得到一個錯誤:

WARN org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - Failed to evaluate Jackson deserialization for type [[simple type, class com.webserverconfig.user.entity.AutoService]]: java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': no back reference property found from type [collection type; class java.util.List, contains [simple type, class com.webserverconfig.user.entity.Service]] 

下一頁兩類代表我的模型:

類汽車維修:

@Entity 
@Table(name = "AutoRate") 
public class AutoService { 

    public AutoService() { 
    } 

    @Id 
    @GeneratedValue(generator = "increment") 
    @GenericGenerator(name = "increment", strategy = "increment") 
    private long id; 

    @Column(name = "serviceName", nullable = false) 
    private String serviceName; 

    @Column(name = "imageURL", nullable = false) 
    private String imageURL; 

    @Column(name = "mapCoordinate", nullable = false) 
    private String mapCoordinate; 

    @Column(name = "websiteURL", nullable = false) 
    private String websiteURL; 

    @Column(name = "phoneNumber", nullable = false) 
    private String phoneNumber; 

    @JsonManagedReference 
    @OneToMany(fetch = FetchType.EAGER) 
    @JoinColumn(name = "autoServiceId") 
    private List<Service> services; 

    public long getId() { 
     return id; 
    } 

    public String getServiceName() { 
     return serviceName; 
    } 

    public String getImageURL() { 
     return imageURL; 
    } 

    public String getMapCoordinate() { 
     return mapCoordinate; 
    } 

    public String getWebsiteURL() { 
     return websiteURL; 
    } 

    public String getPhoneNumber() { 
     return phoneNumber; 
    } 

    public List<Service> getServices() { 
     return services; 
    } 
} 

班級服務:

@Entity 
@Table(name = "Service") 
public class Service { 

    public Service() { 
    } 

    @Id 
    @GeneratedValue(generator = "increment") 
    @GenericGenerator(name = "increment", strategy = "increment") 
    @Column(name = "serviceId", unique = true, nullable = false) 
    private long serviceId; 

    @Column(name = "serviceName", nullable = false) 
    private String serviceName; 

    @Column(name = "category", nullable = false) 
    private String category; 

    @Column(name = "price", nullable = false) 
    private int price; 

    @Column(name = "autoServiceId", nullable = false) 
    private long autoServiceId; 

    public long getId() { 
     return serviceId; 
    } 

    public String getCategory() { 
     return category; 
    } 


    public int getPrice() { 
     return price; 
    } 

    public String getServiceName() { 
     return serviceName; 
    } 

    public long getAutoServiceId() { 
     return autoServiceId; 
    } 
} 

尋求幫助。我失去了一些註解?

而且控制器類:

@RestController 
@RequestMapping("/directory") 
public class ServiceController { 

    @Autowired 
    private AutoRateService dataBaseService; 

    @RequestMapping(value = "/get", method = RequestMethod.GET) 
    @ResponseBody 
    public AutoService getData(){ 
     AutoService dataList = dataBaseService.getById(1); 
     return dataList; 
    } 

    @RequestMapping(value = "/saveService", method = RequestMethod.POST) 
    @ResponseBody public AutoService saveAutoService(@RequestBody AutoService autoService){ 
     return dataBaseService.save(autoService); 
    } 
} 

回答

1

你可以添加@JsonBackReference的關係的其他部位。它的方式丟失或不正確執行。地址:

@JsonBackReference 
@ManyToOne 
@JoinColumn(name = "autoServiceId", nullable = false) 
private AutoService autoService; 

,而不是private long autoServiceId;

另外,汽車維修需要被調整:

@JsonManagedReference 
@OneToMany(mappedBy = "autoService", fetch=FetchType.EAGER) 
private List<Service> services = new ArrayList<>();