status ": 400 com.fasterxml.jackson.databind.exc.MismatchedInputException: Невозможно десериализовать экземпляр java.lang.Boolean - PullRequest
0 голосов
/ 08 февраля 2019

Я работаю с загрузочным приложением Spring и запускаю команду 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);
    }

Цель состоит в том, чтобы найти встречу и обновить статус доступности.

Модель также предоставляется,

@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
    @JsonProperty("status")
    private Boolean 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") boolean 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, boolean 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 boolean isStatus() {
        return status;
    }

    public void setStatus(boolean 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 isStatus() == that.isStatus() &&
                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());
    }

    @Override
    public int hashCode() {

        return Objects.hash(getId(), getCraeted_at(), getAppointment_date(), getName_of_doctor(), isStatus(), 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 +
                '}';
    }
}

Может кто-нибудь помочьопределить, почему доставляется исключение MismatchedInputException?

Ответы [ 2 ]

0 голосов
/ 08 февраля 2019

Я создал класс 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;
    }
}

Изменен вызов API для PUT,

@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 через терминал,

$ curl -X PUT -H "Content-Type: application/json" -d "{\"status\" : \"false\"}" http://localhost:8080/api/v1/appointments/1/update
0 голосов
/ 08 февраля 2019

Я решил это с помощью @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);
    }

Я делаю вызов следующим образом, который, кажется, работает.

$ curl -X PUT -H "Content-Type: application/json" http://localhost:8080/api/v1/appointments/1/update?status=false
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...