当前位置: 首页 > 知识库问答 >
问题:

无法评估Spring Bootapi中的Jackson反序列化

谢俊英
2023-03-14
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 Wed14:17:56.475WARN MappingJackson2HttpMessageConzer无法评估Jackson反序列化类型[[简单类型,类lk.andunaechomedia.models.设备]]:com.fasterxml.jackson.databind.JsonMappingExc0019:多个返回引用属性与名称'user-运动'20-01-22周三14:17:56.475WARN DefaultHandlerExceptionResolver已解决[org.springframework.web.HttpMediaTypeNotSupport dExcture: Content type'应用程序/json; charset=UTF-8'不支持]

这是我的请求主体{“设备id”:“bus009”,“客户名称”:“anura”,“起点”:“matara”,“终点”:“colombo”,“设备地址”:“dikwella”,“电话号码”:“0712345678”,“发布日期”:“2020-10-10”,“设备组”:“138总线”}

共有2个答案

於子晋
2023-03-14
 @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;



谷泳
2023-03-14

在对象树中不止一次引用名称user-mobile作为@JsonBack参考。你需要给其中一个重命名。这两种情况是:

@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:名为“defaultReference”的多个反向引用属性

 类似资料:
  • 我试图在电影和用户之间建立多对多的关系。当我保存电影时,我会收到这个错误: 2017-12-01 16:12:43.351警告17328---[nio-8090-exec-5]。c、 j.MappingJackson2HttpMessageConverter:未能评估类型[[simple type,class com.movieseat.models.Movie]]:java的Jackson反序列

  • 添加到DTO对象后,我想向服务器发送一个列表 从…起 当向控制器发送对象时,它会抛出

  • 问题:假客户机对返回的Spring boot Rest API进行API调用时,无法反序列化该页的属性。 Spring Boot:2.3.3.发布 春云假:2.2.5.发布 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造的实例(没有像默认构造函数一样的创建者存在):无法从[源:(BufferedReader);行

  • 问题内容: 我正在尝试使用Jackson将json数据转换为POJO对象。这是MainActivity和我的POJO类代码。我基本上收到了JsonMappingException错误。我还附上了整个日志。 MainActivity.java: Entries.java(这是POJO) 现在,我的日志中出现以下错误。因此,我无法继续工作。这是日志: 问题答案: 在 条目* 和 电话中 删除构造函数

  • 代码试图采用java.awt.color类使用jackson对象映射器对其进行序列化。获取生成的json字符串并将其反序列化回java.awt.color类。但是在进行反序列化时会出现以下错误。 线程"main"com.fasterxml.jackson.databind.JsonMappingException中的异常:找不到适合类型[简单类型,类java.awt.颜色]的构造函数:无法从JSO