I set up a Spring Microservice app with a POST endpoint which takes in a RequestBody object which contains a 'java.util.Date' field. From my other app, when I send a request to this POST endpoint, the Date in the JSON looks good, in the format "2019-06-20T13:33:47.487-07:00", but when my microservice receives the request, it converts the Date String into the following: "Jun 20, 2019 1:33:47 PM" and tells me "Cannot deserialize value of type java.util.Date
from String "Jun 20, 2019 1:33:47 PM": not a valid representation".
I've played around with different annotations, including "@Temporal(TemporalType.TIMESTAMP)" and "@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)". Also tried setting JSONFormat manually, but that isn't a fix, since the input date string is getting reinterpreted somehow somewhere.
Here's the microservice POST endpoint:
@PostMapping(path = "/orders/create")
public ResponseEntity createOrder(@RequestBody final HubOrder hubOrder) {
List<OrderLineitem> orderLineitem = orderService.hubOrderToOrderLineitems(hubOrder);
repository.saveAll(orderLineitem);
return ResponseEntity.created().build();
}
And bean:
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class HubOrder {
private BigInteger id;
private Date created;
private String lastModifiedBy;
private List<HubLineitem> lineitems;
}
And I hit the endpoint from my app like so:
HubOrder hubOrder = createHubOrderFromOrder(order);
Gson gson = new Gson();
HttpPost request = new HttpPost("http://localhost:8888/orders/create");
StringEntity params = new StringEntity(gson.toJson(hubOrder));
HttpClient httpClient = HttpClientBuilder.create().build();
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
Where hubOrder.getCreated() == "2019-06-20T13:33:47.487-07:00"
The error from my microservice is "2019-07-08 20:50:47.663 WARN 2400 --- [nio-8888-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.util.Date
from String "Jun 20, 2019 1:33:47 PM": not a valid representation (error: Failed to parse Date value 'Jun 20, 2019 1:33:47 PM': Unparseable date: "Jun 20, 2019 1:33:47 PM"); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.util.Date
from String "Jun 20, 2019 1:33:47 PM": not a valid representation (error: Failed to parse Date value 'Jun 20, 2019 1:33:47 PM': Unparseable date: "Jun 20, 2019 1:33:47 PM")
"