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

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

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(&quot;dateTime outside if::&quot; + 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(&quot;countryCode&quot;) String countryCode,
			@PathParam(&quot;workOrderId&quot;) int workOrderId, @QueryParam(&quot;bookingTime&quot;) String bookingTime,
			@QueryParam(&quot;reasonId&quot;) int reasonId) throws CDException {
		LocalDateTime localDateTime = LocalDateTime.parse(bookingTime, DateTimeFormatter.ISO_DATE_TIME);
		logger.debug(&quot;bookingTime:: {}&quot;, bookingTime);
return Response.ok(true).build();

}

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:

确定