“地位”:400 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化java.lang.Boolean的实例

“地位”:400 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化java.lang.Boolean的实例,java,rest,spring-boot,jackson,Java,Rest,Spring Boot,Jackson,我使用Spring boot应用程序,通过终端运行cURL命令 $ curl -X PUT -H "Content-Type: application/json" -d "{\"status\" : \"false\"}" http://localhost:8080/api/v1/appointments/1 我得到了回复 {"timestamp":"2019-02-08T16:23:32.235+0000","status":400,"error":"Bad Request","messag

我使用Spring boot应用程序,通过终端运行
cURL
命令

$ curl -X PUT -H "Content-Type: application/json" -d "{\"status\" : \"false\"}" http://localhost:8080/api/v1/appointments/1
我得到了回复

{"timestamp":"2019-02-08T16:23:32.235+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize instance of java.lang.Boolean out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.Boolean out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]","path":"/api/v1/appointments/1"}
API如下所示:

@PutMapping("/{id}")
    @JsonDeserialize(as = Boolean.class)
    public ResponseEntity<Appointment> updateAppointmentStatus(@PathVariable Long id, @RequestBody Boolean status) {

        Appointment appointment = service.findById(id).get();
        appointment.setStatus(status);

        service.save(appointment);

        return ResponseEntity.status(HttpStatus.ACCEPTED).body(appointment);
    }

有人能帮我找出为什么会出现不匹配的PutException吗?

我用
@RequestParam
解决了这个问题,并更新了API

@PutMapping("/{id}/update/")
    public ResponseEntity<Appointment> updateAppointmentStatus(@PathVariable Long id, @RequestParam("status") Boolean status) {

        Appointment appointment = service.findById(id).get();
        appointment.setStatus(status);

        service.save(appointment);

        return ResponseEntity.status(HttpStatus.ACCEPTED).body(appointment);
    }

我创建了一个Status类,并将其嵌入到Appointment对象中,以便工作。提供了代码

@Entity
public class Appointment {


//    id
//    created_at
//    appointment_date
//    name_of_doctor
//    status (Available or Booked)
//    price

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private java.sql.Time craeted_at;

    @Column
    private java.sql.Date appointment_date;

    @Column
    private String name_of_doctor;

//    @Column
//    private Boolean status;

    @Embedded
    private Status status;

    @Column
    private double price;

    public Appointment() {

    }

    @JsonCreator
    public Appointment(@JsonProperty("craeted_at") Time craeted_at, @JsonProperty("appointment_date") Date appointment_date,
                       @JsonProperty("name_of_doctor") String name_of_doctor, @JsonProperty("status") Status status, @JsonProperty("price") double price) {

        this.craeted_at = craeted_at;
        this.appointment_date = appointment_date;
        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }

    public Appointment(String name_of_doctor, Status status, double price) {

        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }

    public Appointment(String name_of_doctor, double price) {

        this.name_of_doctor = name_of_doctor;
        this.price = price;
    }

    public Long getId() {
        return id;
    }

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

    public Time getCraeted_at() {
        return craeted_at;
    }

    public void setCraeted_at(Time craeted_at) {
        this.craeted_at = craeted_at;
    }

    public Date getAppointment_date() {
        return appointment_date;
    }

    public void setAppointment_date(Date appointment_date) {
        this.appointment_date = appointment_date;
    }

    public String getName_of_doctor() {
        return name_of_doctor;
    }

    public void setName_of_doctor(String name_of_doctor) {
        this.name_of_doctor = name_of_doctor;
    }

    public Status isStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Appointment)) return false;
        Appointment that = (Appointment) o;
        return Double.compare(that.getPrice(), getPrice()) == 0 &&
                Objects.equals(getId(), that.getId()) &&
                Objects.equals(getCraeted_at(), that.getCraeted_at()) &&
                Objects.equals(getAppointment_date(), that.getAppointment_date()) &&
                Objects.equals(getName_of_doctor(), that.getName_of_doctor()) &&
                Objects.equals(status, that.status);
    }

    @Override
    public int hashCode() {

        return Objects.hash(getId(), getCraeted_at(), getAppointment_date(), getName_of_doctor(), status, getPrice());
    }

    @Override
    public String toString() {
        return "Appointment{" +
                "id=" + id +
                ", craeted_at=" + craeted_at +
                ", appointment_date=" + appointment_date +
                ", name_of_doctor='" + name_of_doctor + '\'' +
                ", status=" + status +
                ", price=" + price +
                '}';
    }
}




import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Status {

    @Column(name = "status")
    Boolean status;

    public Status() {
    }

    public Status(Boolean status) {
        this.status = status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }
}
PUT的API调用已更改

@PutMapping("/{id}/update")
    @JsonDeserialize(as = Boolean.class)
    public ResponseEntity<Appointment> updateAppointmentStatus(@PathVariable Long id, @RequestBody Status status) {

        Appointment appointment = service.findById(id).get();
        appointment.setStatus(status);

        service.save(appointment);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(appointment);
    }

似乎您正在尝试将字符串值解析为布尔值,您是否尝试过
“{\”status\”:false}”
?我刚刚尝试过,但它仍然提供了相同的错误堆栈。即使你尝试了
“{\“status\”:False}”
也无法正常工作,你必须创建一个简单的
POJO
类状态{Boolean status;}
,它应该可以工作。@MichałZiober我写了一个POJO,这终于为我解决了。
@Entity
public class Appointment {


//    id
//    created_at
//    appointment_date
//    name_of_doctor
//    status (Available or Booked)
//    price

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private java.sql.Time craeted_at;

    @Column
    private java.sql.Date appointment_date;

    @Column
    private String name_of_doctor;

//    @Column
//    private Boolean status;

    @Embedded
    private Status status;

    @Column
    private double price;

    public Appointment() {

    }

    @JsonCreator
    public Appointment(@JsonProperty("craeted_at") Time craeted_at, @JsonProperty("appointment_date") Date appointment_date,
                       @JsonProperty("name_of_doctor") String name_of_doctor, @JsonProperty("status") Status status, @JsonProperty("price") double price) {

        this.craeted_at = craeted_at;
        this.appointment_date = appointment_date;
        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }

    public Appointment(String name_of_doctor, Status status, double price) {

        this.name_of_doctor = name_of_doctor;
        this.status = status;
        this.price = price;
    }

    public Appointment(String name_of_doctor, double price) {

        this.name_of_doctor = name_of_doctor;
        this.price = price;
    }

    public Long getId() {
        return id;
    }

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

    public Time getCraeted_at() {
        return craeted_at;
    }

    public void setCraeted_at(Time craeted_at) {
        this.craeted_at = craeted_at;
    }

    public Date getAppointment_date() {
        return appointment_date;
    }

    public void setAppointment_date(Date appointment_date) {
        this.appointment_date = appointment_date;
    }

    public String getName_of_doctor() {
        return name_of_doctor;
    }

    public void setName_of_doctor(String name_of_doctor) {
        this.name_of_doctor = name_of_doctor;
    }

    public Status isStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Appointment)) return false;
        Appointment that = (Appointment) o;
        return Double.compare(that.getPrice(), getPrice()) == 0 &&
                Objects.equals(getId(), that.getId()) &&
                Objects.equals(getCraeted_at(), that.getCraeted_at()) &&
                Objects.equals(getAppointment_date(), that.getAppointment_date()) &&
                Objects.equals(getName_of_doctor(), that.getName_of_doctor()) &&
                Objects.equals(status, that.status);
    }

    @Override
    public int hashCode() {

        return Objects.hash(getId(), getCraeted_at(), getAppointment_date(), getName_of_doctor(), status, getPrice());
    }

    @Override
    public String toString() {
        return "Appointment{" +
                "id=" + id +
                ", craeted_at=" + craeted_at +
                ", appointment_date=" + appointment_date +
                ", name_of_doctor='" + name_of_doctor + '\'' +
                ", status=" + status +
                ", price=" + price +
                '}';
    }
}




import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Status {

    @Column(name = "status")
    Boolean status;

    public Status() {
    }

    public Status(Boolean status) {
        this.status = status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }
}
@PutMapping("/{id}/update")
    @JsonDeserialize(as = Boolean.class)
    public ResponseEntity<Appointment> updateAppointmentStatus(@PathVariable Long id, @RequestBody Status status) {

        Appointment appointment = service.findById(id).get();
        appointment.setStatus(status);

        service.save(appointment);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(appointment);
    }
$ curl -X PUT -H "Content-Type: application/json" -d "{\"status\" : \"false\"}" http://localhost:8080/api/v1/appointments/1/update