I have a class like this:
@Data
@NoArgsConstructor(force = true)
@AllArgsConstructor(staticName = "of")
public class BusinessPeriodDTO {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
LocalDate startDate;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
LocalDate endDate;
}
And I used this class inside another class, let's call it PurchaseOrder
@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class PurchaseOrder {
@EmbeddedId
PurchaseOrderID id;
@Embedded
BusinessPeriod rentalPeriod;
public static PurchaseOrder of(PurchaseOrderID id, BusinessPeriod period) {
PurchaseOrder po = new PurchaseOrder();
po.id = id;
po.rentalPeriod = period;
return po;
}
And I'm trying to populate a purchaseOrder record using jakson and this JSON:
{
"_class": "com.rentit.sales.domain.model.PurchaseOrder",
"id": 1,
"rentalPeriod": {
"startDate": "2016-10-10",
"endDate": "2016-12-12"
}
}
But I have faced with an error:
java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('2016-10-10');
I am sure jakson and popularization works correctly.
Include in your pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.6</version>
</dependency>
Then in your BusinessPeriodDTO.java
import LocalDateDeserializer
as follows:
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
And finally, always in your BusinessPeriodDTO.java
file, annotate the interested dates like this:
@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate startDate;
@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate endDate;
Old question but I recently had to answer it for myself. There are different solutions (as commented by rapasoft, see for example here ). The quick solution I used involves adding a setDate(String)
method for deserialization. It might not be the prettiest solution, but it works without updating other classes. Below a runnable class to demonstrate:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
/**
* Demonstrate Java 8 date/time (de)serialization for JSON with Jackson databind.
* Requires {@code com.fasterxml.jackson.core:jackson-databind:2.8.5}
* and {@code com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.5}
*/
public class JdateDto {
/** The pattern as specified by {@link java.text.SimpleDateFormat} */
public static final String ISO_LOCAL_DATE_PATTERN = "yyyy-MM-dd";
/* Used when serializing isoLocalDate. */
@JsonFormat(shape = Shape.STRING, pattern = ISO_LOCAL_DATE_PATTERN)
private LocalDate isoLocalDate;
public LocalDate getIsoLocalDate() {
return isoLocalDate;
}
/* Used when deserializing isoLocalDate. */
public void setIsoLocalDate(String date) {
setIsoLocalDate(LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE));
}
public void setIsoLocalDate(LocalDate isoDate) {
this.isoLocalDate = isoDate;
}
public static void main(String[] args) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
JdateDto dto = new JdateDto();
dto.setIsoLocalDate(LocalDate.now());
String json = mapper.writeValueAsString(dto);
System.out.println(json);
JdateDto dto2 = mapper.readValue(json, JdateDto.class);
if (dto.getIsoLocalDate().equals(dto2.getIsoLocalDate())) {
System.out.println("Dates match.");
} else {
System.out.println("Dates do not match!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.