LocalDateTime在向JAX-RS控制器发出的PUT请求中被设置为Null。

huangapple go评论104阅读模式
英文:

LocalDateTime being set to Null in PUT Request to JAX-RS Controller

问题

  1. 我尝试在方法的请求参数中传递 LocalDateTime,但它始终记录为 null。请帮我找出我在这里做错了什么:
  2. @Path("/book/workOrderId/{workOrderId}")
  3. @PUT
  4. public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
  5. @PathParam("workOrderId") int workOrderId,
  6. @QueryParam("reasonId") int reasonId,
  7. @RequestParam(value = "dateTime") @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime dateTime
  8. ) throws CDException {
  9. logger.debug("dateTime outside if::" + dateTime);
  10. return Response.ok(true).build();
  11. }

输出:

  1. 22:32:27.659 [http-nio-8300-exec-2] DEBUG c.c.t.i.e.v.impl.WorkorderController - dateTime outside if::null
  1. <details>
  2. <summary>英文:</summary>
  3. 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

  1. ) throws CDException {
  2. logger.debug(&quot;dateTime outside if::&quot; + dateTime);
  3. return Response.ok(true).build();

}

  1. Output:

22:32:27.659 [http-nio-8300-exec-2] DEBUG c.c.t.i.e.v.impl.WorkorderController - dateTime outside if::null

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 这对我有效:
  5. ```java
  6. @PUT
  7. public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam("countryCode") String countryCode,
  8. @PathParam("workOrderId") int workOrderId, @QueryParam("bookingTime") String bookingTime,
  9. @QueryParam("reasonId") int reasonId) throws CDException {
  10. LocalDateTime localDateTime = LocalDateTime.parse(bookingTime, DateTimeFormatter.ISO_DATE_TIME);
  11. logger.debug("bookingTime:: {}", bookingTime);
  12. return Response.ok(true).build();
  13. }
英文:

This worked for me:

  1. @PUT
  2. public Response bookWorkOrder(@Context HttpHeaders headers, @PathParam(&quot;countryCode&quot;) String countryCode,
  3. @PathParam(&quot;workOrderId&quot;) int workOrderId, @QueryParam(&quot;bookingTime&quot;) String bookingTime,
  4. @QueryParam(&quot;reasonId&quot;) int reasonId) throws CDException {
  5. LocalDateTime localDateTime = LocalDateTime.parse(bookingTime, DateTimeFormatter.ISO_DATE_TIME);
  6. logger.debug(&quot;bookingTime:: {}&quot;, bookingTime);
  7. return Response.ok(true).build();
  8. }

huangapple
  • 本文由 发表于 2020年10月9日 01:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64267987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定