我使用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吗?
答案 0 :(得分:0)
我使用@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
答案 1 :(得分:0)
我创建了一个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