java - 状态“:400 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化java.lang.Boolean的实例
问题描述
我使用 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);
}
目的是查找约会并更新可用性状态。
模型也提供,
@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?
解决方案
我创建了一个 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);
}
cURL
最后,我通过终端拨打电话,
$ curl -X PUT -H "Content-Type: application/json" -d "{\"status\" : \"false\"}" http://localhost:8080/api/v1/appointments/1/update
推荐阅读
- html - 我如何能够将动画 svg 代码保存到 .svg 文件?
- ios - iOS 将不同设备之间的数据共享到同一个应用程序
- java - 是否可以将对象提供给 ListView,但显示字符串?
- java - 使用 JUnit5 时,出现警告:“ClassNotFoundException: org.junit.platform.engine.support.filter.ExclusionReasonConsumingFilter”
- excel - 交叉引用数据表和给定一列和多行引用的数字总和
- php - 在 PHP 中不在对象上下文中时使用 $this
- swift - 如何仅按名称下载 pod?
- c# - 从数组创建 8 位位图
- javascript - 如何检测服务器是否正在运行 node.js?
- function - 为什么我在 Unix shell 中的函数总是返回 0?