Reputation: 1223
I'm using a very simple REST WS
with SpringBoot
and PostMan
to send, receive and display data. Problem is, there seems to be a problem with deserialising Date
value and for some reason, it only accepts this format yyyy-mm-dd
(separated by dashes).
So, for example, if I send the following data from Postman
, I receive the response without a problem:
{
"server":{
"address":"url",
"port":2555525
},
"date":"2019-02-13"
}
However, if I do the following (value date without dashes):
{
"server":{
"address":"url",
"port":2555525
},
"date":"20190213"
}
I get the following error:
{
"timestamp": 1550046097893,
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String \"20190213\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '20190213' could not be parsed at index 0; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String \"20190213\": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '20190213' could not be parsed at index 0\n at [Source: (PushbackInputStream); line: 6, column: 9] (through reference chain: com.restws.webservices.model.Admin[\"date\"])",
"path": "/v3/admin/device"
}
What I would like to achieve is to be able to deserialise the date regardless of the format entered in PostMan
. Is there a way of doing that?
Here's my controller:
@Controller
@RequestMapping("/v3/admin/")
public class AdminController {
Logger logger = LoggerFactory.getLogger(AdminController.class);
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(method = RequestMethod.POST, value = "/device", consumes ="application/json")
public ResponseEntity putDevice(@RequestBody Admin admin) throws IOException {
logger.info(admin.toString());
String adminString = admin.toString();
return new ResponseEntity<>(adminString, HttpStatus.OK);
}
}
And the POJOs:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Admin {
@JsonProperty("server")
private Server server;
@JsonProperty("date")
private LocalDate date;
public Admin() {
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("server", server).append("date", date).toString();
}
}
//
public class Server {
@JsonProperty("address")
private String address;
@JsonProperty("port")
private int port;
public Server() {
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Server server = (Server) o;
return new EqualsBuilder().append(port, server.port).append(address, server.address).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(address).append(port).toHashCode();
}
@Override
public String toString() {
return new ToStringBuilder(this).append("address", address).append("port", port).toString();
}
}
Upvotes: 3
Views: 6949
Reputation: 208
You can try to write custom JsonDeserializer to handle the date deserialization in formats you want to accept: JSON serialization strategy for dates
Upvotes: 2