【问题标题】:Jackson failed to deserialization OneToMany objectsJackson 未能反序列化 OneToMany 对象
【发布时间】:2017-03-05 18:19:22
【问题描述】:

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

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

我有一个包含许多服务的 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]]

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

类自动服务:

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

【问题讨论】:

    标签: java json spring


    【解决方案1】:

    您可以将@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<>();
    

    【讨论】:

      【解决方案2】:

      解决方案 #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({"name-of-your-attribute"}),例如“cmets”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-07
        • 1970-01-01
        • 2022-01-22
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        相关资源
        最近更新 更多