Recently, using spring Cloud to develop a micro-service, the following exception occurred during the test a service call B service:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2018-04-04 14:48:30": not a valid representation (error: Failed to parse Date value '2018-04-04 14:48:30': Can not parse date "2018-04-04 14:48:30": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-04-04 14:48:30": not a valid representation (error: Failed to parse Date value '2018-04-04 14:48:30': Can not parse date "2018-04-04 14:48:30": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS', parsing fails (leniency? null))
at [Source: java.io.PushbackInputStream@4d9b0ce9; line: 1, column: 200] (through reference chain: com.zsagepay.user.dto.UserDto["createDate"]
The scene at the time:
A Tune B service query user interface, return user data (userdto), a modify return user data in the user name and avatar, ID and so on, and then drop B update user interface, update user information. There are parameters such as creation time, update time and so on in Userdto.
The cause of the exception occurred:
b The date format for creation time and update time in user data for A is: ' Yyyy-mm-dd HH:mm:ss ', a when calling B, the request data format is: ' Yyyy-mm-dd HH:mm:ss ', but b when receiving the data, Jackson was required to convert the data into Userdto objects. When Jackson transforms, the default time format is ' yyyy-mm-dd ' T ' HH:mm:ss. SSS ', so there will be an exception above.
Workaround:
Add the following annotation to the date format in Userdto to resolve:
@DateTimeFormat (pattern = "Yyyy-mm-dd HH:mm:ss")
@JsonFormat (pattern = "Yyyy-mm-dd HH:mm:ss")
For example: Create a Time field:
@ApiModelProperty (value = "Create time; do not need to pass")
@DateTimeFormat (pattern = "Yyyy-mm-dd HH:mm:ss")
@JsonFormat (pattern = "Yyyy-mm-dd HH:mm:ss")
private Date createdate;