Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “弹簧启动状态”:415,“;错误";:&引用;不支持的媒体_Java_Rest_Spring Boot_Jackson Databind - Fatal编程技术网

Java “弹簧启动状态”:415,“;错误";:&引用;不支持的媒体

Java “弹簧启动状态”:415,“;错误";:&引用;不支持的媒体,java,rest,spring-boot,jackson-databind,Java,Rest,Spring Boot,Jackson Databind,我使用SpringBootRESTfulAPI,在执行POST请求时,我得到了415错误。下面提供了cURL请求 $ curl -i -X POST -H "Content-Type:application/json" -d "{\"name_of_doctor\" : \"Monika\", \"status\": \"true\", \"price\": \"12.5\"}" http://localhost:8080/api/v1/appointments/createAppointmen

我使用SpringBootRESTfulAPI,在执行POST请求时,我得到了415错误。下面提供了
cURL
请求

$ curl -i -X POST -H "Content-Type:application/json" -d "{\"name_of_doctor\" : \"Monika\", \"status\": \"true\", \"price\": \"12.5\"}" http://localhost:8080/api/v1/appointments/createAppointment
答案就在这里

HTTP/1.1 415 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 08 Feb 2019 09:05:04 GMT

{"timestamp":"2019-02-08T09:05:04.560+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/json;charset=UTF-8' not supported","path":"/api/v1/appointments/createAppointment"}
API处理HTTP请求

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/appointments")
public class AppointmentAPI {

    @Autowired
    private AppointmentService service;

    @GetMapping("/getAll")
    public ResponseEntity<List<Appointment>> findAll() {
        return ResponseEntity.ok(service.findAll());
    }

    @PostMapping(value = "/createAppointment", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResponseEntity<Appointment> create(@RequestBody Appointment appointment) {

        java.sql.Date date = new java.sql.Date(Calendar.getInstance().getTime().getTime());
        java.sql.Time time = new java.sql.Time(Calendar.getInstance().getTime().getTime());

        appointment.setAppointment_date(date);
        appointment.setCraeted_at(time);

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


}
运行窗口提供了错误堆栈

2019-02-08 10:10:24.499  INFO 1337 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-02-08 10:10:24.499  INFO 1337 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-02-08 10:10:24.526  INFO 1337 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 26 ms
2019-02-08 10:10:24.640  WARN 1337 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.appoint.manager.appointment.models.Appointment]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class com.appoint.manager.appointment.models.Appointment does not have build method (name: 'build')
2019-02-08 10:10:24.642  WARN 1337 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.appoint.manager.appointment.models.Appointment]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class com.appoint.manager.appointment.models.Appointment does not have build method (name: 'build')
2019-02-08 10:10:24.645  WARN 1337 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]

有人能告诉我这里出了什么问题吗?

您之所以会出现此错误,是因为您指定了
@JsonDeserialize(builder=Appointment.class)
。这告诉jackson使用builder Appointment.class(不是构建器)来反序列化负载。 尝试提供valide生成器或使用@JsonCreator,如:

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

之所以出现此错误,是因为您指定了
@JsonDeserialize(builder=Appointment.class)
。这告诉jackson使用builder Appointment.class(不是构建器)来反序列化负载。 尝试提供valide生成器或使用@JsonCreator,如:

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

这个问题是在否决投票后编辑的,我在发表这篇文章之前检查了类似的答案。你有任何stacktrace吗?是的,很抱歉之前的回答。它会通知
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
的错误,请尝试删除
@JsonDeserialize(builder=Appointment.class)
,您正在将
Appointment
指定为生成器类,但该类无效。好的,谢谢@Arnaud。。。问题解决了。问题在否决投票后被编辑,我在发表这篇文章之前检查了类似的答案。你有stacktrace吗?是的,很抱歉之前的回复。它会通知
com.fasterxml.jackson.databind.exc.InvalidDefinitionException
的错误,请尝试删除
@JsonDeserialize(builder=Appointment.class)
,您正在将
Appointment
指定为生成器类,但该类无效。好的,谢谢@Arnaud。。。问题解决了。