【问题标题】:Failed to evaluate Jackson deserialization in spring boot api无法评估 Spring Boot api 中的 Jackson 反序列化
【发布时间】:2020-05-08 09:28:25
【问题描述】:
package lk.andunaechomedia.models;

import com.fasterxml.jackson.annotation.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import java.util.Date;


@Entity
public  class Device {
    @Id
    private String device_id;
    private String customer_name;
    private String start_point;
    private String end_point;
    private String device_address;
    private String tel_number;
    private Date   publish_date;
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="device_group_group_id")
    private Device_group device_group;

    public Device() {
    }

    public Device(String device_id,String customer_name,String start_point, String end_points, String device_address, String tel_number, Date publish_date) {
        this.setDevice_id(device_id);
        this.setCustomer_name(customer_name);
        this.setStart_point(start_point);
        this.setEnd_point(end_points);
        this.setDevice_address(device_address);
        this.setTel_number(tel_number);
        this.setPublish_date(publish_date);

    }

    public Device_group getDevice_group() {
        return device_group;
    }

    public void setDevice_group(Device_group device_group) {
        this.device_group = device_group;
    }

    public String getDevice_id() {
        return device_id;
    }

    public void setDevice_id(String device_id) {
        this.device_id = device_id;
    }

    public String getCustomer_name() {
        return customer_name;
    }

    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    public String getStart_point() {
        return start_point;
    }

    public void setStart_point(String start_point) {
        this.start_point = start_point;
    }

    public String getEnd_point() {
        return end_point;
    }

    public void setEnd_point(String end_point) {
        this.end_point = end_point;
    }

    public String getDevice_address() {
        return device_address;
    }

    public void setDevice_address(String address) {
        this.device_address = address;
    }

    public String getTel_number() {
        return tel_number;
    }

    public void setTel_number(String telphone) {
        this.tel_number = telphone;
    }

    public Date getPublish_date() {
        return publish_date;
    }

    public void setPublish_date(Date publish_date) {
        this.publish_date = publish_date;
    }

    /*public void updatePartial(Device device, String deice_id){
        Device newDevice = deviceRepo.findOne(device_id);
        if (device.getCustomer_name() != null){
            newDevice.setCustomer_name(device.getCustomer_name());
        }
        if (device.getStart_point() != null){
            newDevice.setStart_point(device.getStart_point());
        }
        if (device.getEnd_point() != null){
            newDevice.setEnd_point(device.getEnd_point());
        }
        if (device.getDevice_address() != null){
            newDevice.setDevice_address(device.getDevice_address());
        }
        if (device.getTel_number() != null){
            newDevice.setTel_number(device.getTel_number());
        }
        if (device.getPublish_date() != null){
            newDevice.setPublish_date(device.getPublish_date());
        }
    }*/


}




package lk.andunaechomedia.models;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.util.Set;

@Entity
public class Device_group {
   @Id
    private String group_id;
    private String group_name;
    @OneToMany(mappedBy = "device_group")
    private Set<Device> devices;
    @ManyToOne
    @JoinColumn(name="device_group_group_id", nullable=false)

    private Main_schedule main_schedule;
    private String temp_schedule_temp_id;
    @JsonBackReference(value="user-movement")
    public Set<Device> getDevice() {
        return devices;
    }

    public void setDevice(Set<Device> devices) {
        this.devices = devices;
    }

    public String getGroup_id(){ return group_id; }
     public void setGroup_id(String group_id){
      this.group_id=group_id;
     }

    public String getGroup_name(){
      return group_name;
     }
    public void setGroup_name(String group_name){
      this.group_name=group_name;
     }
    @JsonBackReference(value="user-movement")
    public Main_schedule getMain_schedule_schedule_id(){
      return main_schedule;
     }
    public void setMain_schedule_schedule_id(Main_schedule main_schedule){
      this.main_schedule=main_schedule;
     }

    public String getTemp_schedule_temp_id(){
      return temp_schedule_temp_id;
     }
    public void setTemp_schedule_temp_id(String temp_schedule_temp_id){
      this.temp_schedule_temp_id=temp_schedule_temp_id;
     }
}

这是错误 20-01-22 星期三 14:17:56.475 警告 MappingJackson2HttpMessageConverter 无法评估杰克逊反序列化类型 [[简单类型, 类 lk.andunaechomedia.models.Device]]: com.fasterxml.jackson.databind.JsonMappingException:多个 名称为“用户移动”的反向引用属性 20-01-22 Wed 14:17:56.475 WARN DefaultHandlerExceptionResolver 已解决 [org.springframework.web.HttpMediaTypeNotSupportedException:内容 type 'application/json;charset=UTF-8' not supported]

这是我的请求正文 { "device_id":"bus009", "customer_name":"anura", “起点”:“马塔拉”,“终点”:“科伦坡”, “设备地址”:“dikwella”,“电话号码”:“0712345678”, "publish_date":"2020-10-10", "device_group":"138-Bus" }

【问题讨论】:

标签: spring-boot spring-data-jpa


【解决方案1】:

您在对象树中多次将名称 user-movement 引用为 @JsonBackReference。您需要重命名其中之一。这两种情况是:

@JsonBackReference(value="user-movement")
    public Set<Device> getDevice() {
        return devices;
    }

@JsonBackReference(value="user-movement")
    public Main_schedule getMain_schedule_schedule_id(){
      return main_schedule;
     }

查看更多: Jackson: Multiple back-reference properties with name 'defaultReference'

【讨论】:

    【解决方案2】:
     @JsonBackReference  
       @OneToMany(fetch=FetchType.LAZY,  
         mappedBy = "device_group" 
           cascade=cascadeType.PERSIST)  
           private Set<Device> devices; 
    
    
     @JsonManagedReference    
         @ManyToOne(fetch=FetchType.LAZY)  
            @JoinColumn(name="device_group_group_id")  
            private Device_group device_group;
    
    
    
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    • 在没有 JsonBackReference 的 OneToMany 和 JsonManagedReference 的 @ManyToOne 的情况下修复了此错误,但需要从数据库获取数据
    猜你喜欢
    • 2018-11-14
    • 2023-04-11
    • 2020-01-09
    • 2019-04-03
    • 2018-01-24
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多