0
I’m doing a test client in java.
My book class is like this:
public class Livro {
private Long id;
private String nome;
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern="dd/MM/yyyy")
private LocalDate publicacao;
private String editora;
private String resumo;
private List<Comentario> comentarios;
private Autor autor;
// get and sets
}
I get the seginte json.
[
{
"id": 1,
"nome": "Livro de teste 2",
"publicacao": "20/05/2014",
"editora": "teste",
"resumo": "teste de resumo"
}
]
If I use the field publicacao
as Date
works perfectly.
But I’d like to use as Localdate
as defined in Class
However the following error.
Caused by: java.time.format.Datetimeparseexception: Text '20/05/2014' could not be Parsed at index 0 at java.time.format.DateTimeFormatter.parseResolved0(Datetimeformatter.java:1949) at java.time.format.DateTimeFormatter.parse(Datetimeformatter.java:1851) at java.time.Localdate.parse(Localdate.java:400) at java.time.Localdate.parse(Localdate.java:385) at com.fasterxml.Jackson.datatype.jsr310.deser.Localdatedeserializer.deserialize(Localdatedeserializer.java:67) at com.fasterxml.Jackson.datatype.jsr310.deser.Localdatedeserializer.deserialize(Localdatedeserializer.java:32) at com.fasterxml.Jackson.databind.deser.SettableBeanProperty.deserialize(Settablebeanproperty.java:490) at com.fasterxml.Jackson.databind.deser.impl.Methodproperty.deserializeAndSet(Methodproperty.java:95) at com.fasterxml.Jackson.databind.deser.BeanDeserializer.deserializeFromObject(Beandeserializer.java:341) ... 14 more
That’s how I parse a json field "publicacao": "20/05/2014"
,
to a field Localdate
?
To make the client I am using the following dependencies in Maven.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>