Java Rest Client Receiving a Localdate

Asked

Viewed 928 times

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>

2 answers

2

Opa!

Currently in my application I am doing as follows:

@JsonFormat(pattern = "dd/MM/yyyy", shape = JsonFormat.Shape.STRING)
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@Column(name = "data_vencimento")
private LocalDate dataVencimento;

I hope I’ve helped!

1


Change the dependency version jackson-datatype-jsr310 for the same version of the dependency jackson-databind:

<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.7.2</version>
</dependency>

And register the module JavaTimeModule instead of the module JSR310Module. Example:

public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());

    Livro livro = mapper.readValue("{\"id\": 1,"
                                + "\"nome\": \"Livro de teste 2\", "
                                + "\"publicacao\": \"20/05/2014\","
                                + "\"editora\": \"teste\","
                                + "\"resumo\": "
                                + "\"teste de resumo\"}", Livro.class);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.