英文:
LocalDateTime being set to Null in PUT Request to JAX-RS Controller
问题
我尝试在方法的请求参数中传递 LocalDateTime,但它始终记录为 null。请帮我找出我在这里做错了什么:
@Path("/book/workOrderId/{workOrderId}")
@PUT
public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
@PathParam("workOrderId") int workOrderId,
@QueryParam("reasonId") int reasonId,
@RequestParam(value = "dateTime") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime dateTime
) throws CDException {
logger.debug("dateTime outside if::" + dateTime);
return Response.ok(true).build();
}
输出:
22:32:27.659 [http-nio-8300-exec-2] DEBUG c.c.t.i.e.v.impl.WorkorderController - dateTime outside if::null
<details>
<summary>英文:</summary>
I am trying to pass LocalDateTime in the request param of a method but it always logs null. Please help me figure out what I did wrong here:
@Path("/book/workOrderId/{workOrderId}")
@PUT
public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
@PathParam("workOrderId") int workOrderId,
@QueryParam("reasonId") int reasonId,
@RequestParam(value = "dateTime") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime dateTime
) throws CDException {
logger.debug("dateTime outside if::" + dateTime);
return Response.ok(true).build();
}
Output:
22:32:27.659 [http-nio-8300-exec-2] DEBUG c.c.t.i.e.v.impl.WorkorderController - dateTime outside if::null
</details>
# 答案1
**得分**: 0
这对我有效:
```java
@PUT
public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
@PathParam("workOrderId") int workOrderId, @QueryParam("bookingTime") String bookingTime,
@QueryParam("reasonId") int reasonId) throws CDException {
LocalDateTime localDateTime = LocalDateTime.parse(bookingTime, DateTimeFormatter.ISO_DATE_TIME);
logger.debug("bookingTime:: {}", bookingTime);
return Response.ok(true).build();
}
英文:
This worked for me:
@PUT
public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
@PathParam("workOrderId") int workOrderId, @QueryParam("bookingTime") String bookingTime,
@QueryParam("reasonId") int reasonId) throws CDException {
LocalDateTime localDateTime = LocalDateTime.parse(bookingTime, DateTimeFormatter.ISO_DATE_TIME);
logger.debug("bookingTime:: {}", bookingTime);
return Response.ok(true).build();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论