Client Rest in Java receiving a LocalDate

0

I'm doing a test client in java.

My workbook class looks 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 json seginte.

[
  {
    "id": 1,
    "nome": "Livro de teste 2",
    "publicacao": "20/05/2014",
    "editora": "teste",
    "resumo": "teste de resumo"
  }
]

If you use the publicacao field as Date it works perfectly. But I would like to use as Localdate as defined in Class However the following error occurs.

  

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

In other words, how do I parse a field json "publicacao": "20/05/2014" for a Localdate field?

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>
    
asked by anonymous 06.03.2017 / 00:28

2 answers

1

Change the dependency version jackson-datatype-jsr310 to the same version as 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);
}
    
06.03.2017 / 01:44
2

Oops!

Currently in my application I'm doing the following:

@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 have helped!

    
06.03.2017 / 02:20