简体   繁体   中英

Cannot deserialize from String value Spring-Boot Restful project

I'm working on a Spring Boot + Maven + Restful + Hibernate project! After creating the RestController for adding new Devices in database i'm getting this error:

    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"])

Here is my first entity:

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

And the second entity:

@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:

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

So when i'm trying to add a new device, i get the error i mentioned upper.I don't know what is causing this exception. When I try to add with post a new Protocol it's working, table is being created in the database correctly and I am getting the data correctly in GET request as well..Please help me, I'm new to springboot and restful... if some more information is required, please just inform me and i will post it! Thank you!

I tried to reproduce your problem: here , but everything works as expected. 休息反应

I think it can be related with this bug .

You should try to reproduce bug with different jackson version.

EDIT:

One more thing: It looks like you try to construct Protocol instead of Device. Show us your deviceService, if you can.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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