繁体   English   中英

无法从字符串值Spring-Boot Restful项目反序列化

[英]Cannot deserialize from String value Spring-Boot Restful project

我正在做一个Spring Boot + Maven + Restful + Hibernate项目! 创建用于在数据库中添加新设备的RestController后,出现此错误:

    2018-03-28 10:15:18.786  WARN 9286 --- [nio-9090-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.hhm.hsy.hibernate.models.Protocol` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":5,"protocolName":"ProtocolForTesting","port":5202}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.hhm.hsy.hibernate.models.Protocol` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":5,"protocolName":"ProtocolForTesting","port":5202}')
 at [Source: (PushbackInputStream); line: 1, column: 52] (through reference chain: com.hhm.hsy.hibernate.models.Device["protocol"])

这是我的第一个实体:

@Entity
@Table(name = "devices", catalog = "hqm")
public class Device implements Serializable {

    private static final long serialVersionUID = -8311225474375837513L;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "device_id", unique = true, nullable = false)
    private Integer id;

    @Column(name = "device_name", unique = true, nullable = false)
    private String deviceName;

    @ManyToOne
    @JoinColumn(name = "protocol_id")
    private Protocol protocol;

    public Device() {
    }

    public Device(Integer id, String deviceName, Protocol protocol) {
        this.id = id;
        this.deviceName = deviceName;
        this.protocol = protocol;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public Protocol getProtocol() {
        return protocol;
    }

    public void setProtocol(Protocol protocol) {
        this.protocol = protocol;
    }

第二个实体:

@Entity
@Table(name = "protocols", catalog = "hqm")
public class Protocol implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "protocol_id", unique = true, nullable = false)
    private Integer id;

    @Column(name = "protocol_name", unique = true, nullable = false, length = 45)
    private String protocolName;

    @Column(name = "port", nullable = false)
    private Integer port;

    @OneToMany(mappedBy = "protocol", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Device> devices = new HashSet<>();

    public Protocol() {
    }

    public Protocol(Integer id, String protocolName, Integer port) {
        this.id = id;
        this.protocolName = protocolName;
        this.port = port;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getProtocolName() {
        return protocolName;
    }

    public void setProtocolName(String protocolName) {
        this.protocolName = protocolName;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    @JsonIgnore
    public Set<Device> getDevices() {
        return devices;
    }

    @JsonIgnore
    public void setDevices(Set<Device> devices) {
        this.devices = devices;
    }

}

控制器:

@RestController
@RequestMapping(value = "/api/devices")
@ComponentScan({"com.hhm.hsy.pmcs.*"})
public class DevicesController {

    @Autowired
    @Qualifier(value = "deviceService")
    GenericServiceIntf deviceService;

    // get ALL DEVICE
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Map<String, Object> getDevices() {
        Map<String, Object> devicesMap = new HashMap<>();
        devicesMap.put("devices", deviceService.getAll());
        return devicesMap;
    }

    //save a new DEVICE
    @RequestMapping(value = "", method = RequestMethod.POST, consumes = {"application/json"}, produces = {"application/json"})
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<Device> addDevice(@RequestBody Device device) {
        deviceService.save(device);
        return ResponseEntity.accepted().body(device);

    }
}

服务:

@Service("deviceService")
public class DeviceServiceImpl extends GenericServiceAbstractImpl<Device, Integer> implements Serializable{

    private static final long serialVersionUID = 697655212967127150L;

    @Autowired
    public DeviceServiceImpl(@Qualifier("deviceDao") GenericDaoIntf genericDao) {
        super(genericDao);
    }
}

因此,当我尝试添加新设备时,出现上面提到的错误。我不知道是什么导致了此异常。 当我尝试添加新协议以使其工作时,表正在数据库中正确创建,并且我也在GET请求中正确获取数据..请帮助我,我是springboot和restful的新手。如果需要更多信息,请通知我,我将其发布! 谢谢!

我试图重现您的问题: 在这里 ,但一切正常。 休息反应

我认为这可能与该错误有关

您应该尝试使用其他杰克逊版本重现错误。

编辑:

还有一件事:看起来您尝试构造协议而不是设备。 向我们显示您的deviceService。

无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:无法构造`com.hhm.hsy.hibernate.models.Protocol的实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM