杰克逊无法反序列化OneToMany对象

时间:2017-03-05 18:19:23

标签: java json spring

我遇到过转换器无法处理JSON对象的问题。

我在数据库中有两个对象。关系OneToMany。

我有一个包含许多服务的AutoService。

我正在尝试使用邮递员将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]]

接下来两个类代表我的模型:

Class AutoService:

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

寻求帮助。我错过了一些注释吗?

Controller类:

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

2 个答案:

答案 0 :(得分:4)

您可以将@JsonBackReference添加到关系的其他网站。顺便说一下,缺少或不正确实施。添加:

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

而不是private long autoServiceId;

此外,需要使用以下内容调整AutoService:

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

答案 1 :(得分:1)

解决方案1: -在具有@OneToMany属性的位置添加@JsonIgnore 示例:

class User {
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonManagedReference
    @JsonIgnore
    private List<Comment> comments;
}

class Comment {
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    @JsonBackReference
    private User user;
}

解决方案2: -在您的课程@JsonIgnoreProperties({“您的名字的属性”}}上使用,例如“评论”