无法从String值反序列化Spring-Boot Restful项目

时间:2018-03-28 10:54:38

标签: java json hibernate spring-boot

我正在开发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 ......如果需要更多信息,请告知我,我会发布!谢谢!

1 个答案:

答案 0 :(得分:0)

我尝试重现您的问题:here,但一切都按预期进行。rest response

我认为它可能与this bug有关。

您应尝试使用不同的jackson版本重现错误。

修改

还有一件事:看起来你试图构建Protocol而不是Device。如果可以,请向我们展示您的deviceService。

  

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