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

标签 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;
    }

}

Controller :

@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);
    }
}

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

最佳答案

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

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

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

编辑:

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

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.hhm.hsy.hibernate.models.Protocol

关于java - 无法从 String 值反序列化 Spring-Boot Restful 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532643/

相关文章:

java - 将 "deepToString()"转换回二维数组

javascript - 将 JSON 字符串从 jsp 传递到 js 文件

java - 如何添加子选择来选择

Java哈希表或 HashMap ?

Java Apache HTTP 401 响应返回正确的凭据

java - 从 Java 启动时进程不会退出

python - 通过解析器将 json 文件作为命令行参数传递,这可能吗?

c# - JSON 数组作为空值发送到 Controller

java - 如果实体已经存在,如何通过属性返回实体?

java - 无法获取 LAZY - JPA