【问题标题】:Cannot deserialize from String value Spring-Boot Restful project无法从字符串值 Spring-Boot Restful 项目反序列化
【发布时间】:2018-09-07 00:41:27
【问题描述】:

我正在开发一个 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 的新手并且休息...如果需要更多信息,请告诉我,我会发布!谢谢!

【问题讨论】:

  • 你能提供一个控制器类吗?
  • 我加了!谢谢。

标签: java json hibernate spring-boot


【解决方案1】:

我试图重现您的问题:here,但一切正常。

我觉得可能和this bug有关。

您应该尝试使用不同的杰克逊版本重现错误。

编辑:

还有一件事:看起来您尝试构建协议而不是设备。如果可以,请向我们展示您的 deviceService。

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

【讨论】:

  • 我的杰克逊版本是2.9.4。我试图用 2.9.3 运行它......它没有帮助!关于“你为什么尝试构建协议而不是设备类?” ---- 我正在尝试添加新设备并分配现有协议。
猜你喜欢
  • 2017-05-24
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2017-12-23
  • 2019-08-09
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多