I use Java 11 and want to serialize/deserialize LocalDate/LocalDateTime as String. Okay. I added dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
and module:
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
When I send date to my app, it deserializes correctly:
{"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
When I using objectMapper as bean, directly, it serializes correctly too:
{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":"2008-03-20","relativeType":"SON","cohabitants":true}
But when it serializes with controller, it serializes as array:
{"code":"SUCCESS","id":868,"profileId":12608,"birthDate":[2008,3,20],"relativeType":"SON","cohabitants":true}
Problem is to deserialize date in body on controller. Controller is:
@PostMapping
public Relative create(@Validated(Validation.Create.class) @RequestBody Relative relative) {
return service.create(relative);
}
Relative.class:
@Getter
@Setter
@ToString(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Relative extends MortgageResponse {
@Null(groups = Validation.Create.class)
@NotNull(groups = Validation.Update.class)
private Long id;
@NotNull
private Long profileId;
private LocalDate birthDate;
private RelativeType relativeType;
private Boolean cohabitants;
}
Please, advice me, what's problem and how to fix it.
Note that LocalDate implements Serializable.
How to deserialize Date from JSON using Jackson. In order to correct deserialize a Date field, you need to do two things: 1) Create a custom deserializer by extending StdDeserializer<T> class and override its deserialize(JsonParser jsonparser, DeserializationContext context) method.
No, you cannot extend LocalDate, so that your implementation is a LocalDate in your environment.
1. LocalDate. LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd.
The default serialization and deserialization of java.util.Date and other date-time types such as java.time.LocalDateTime is done in numeric formats. The Date is converted to the long type and other types are converted to string with number values.
LocalDate came with Java 8 and is part of the new standard API in Java for working with dates. However, if you want to effectively use LocalDate over Date in a Spring Boot application, you need to take some extra care, since not all tools support LocalDate by default, yet. Spring Boot includes the popular Jackson library as JSON (de-)serializer.
1. Custom GSON LocalDateSerializer Note that we are formatting default local date "2018-10-26" to "27-Oct-2018". 2. Custom GSON LocalDateTimeSerializer
You can register the module with a Jackson ObjectMapper instance like this: The module teaches the ObjectMapper how to work with LocalDate s and the parameter WRITE_DATES_AS_TIMESTAMPS tells the mapper to represent a Date as a String in JSON.
Add the @JsonFormat annotation to your birthDate field , or rather any date field and your ObjectMapper (Spring Boot or not) should respect the formatting, as long as you have the additional js310 dependency on your classpath.
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate birthDate;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With