1 year ago

#303606

test-img

Anderson

List LocalDate rest spring serialize

I have a response of List<LocalDate> My Class

public class CompetenciaAbertaResponseDTO 
    private List<LocalDate> competencias = new ArrayList<>();

and response JSON

{
  "competencias": [
    {
      "year": 2022,
      "month": "MARCH",
      "monthValue": 3,
      "dayOfMonth": 1,
      "chronology": {
        "id": "ISO",
        "calendarType": "iso8601"
      },
      "dayOfWeek": "TUESDAY",
      "leapYear": false,
      "dayOfYear": 60,
      "era": "CE"
    }
  ]
}

I need read this another service from RestTemplate in spring. Like this

    ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
    String json = response.getBody()
    CompetenciaAbertaResponsetDTO resp =  objectMapper.readValue(json, CompetenciaAbertaResponsetDTO.class);

Error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)    

And when i change for this:

       this.restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://localhost:9001")); 
        String url = RestPath.CONTABIL_FINANCEIRO_SERVICE.listaCompetenciaAbertas;
 ResponseEntity<CompetenciaAbertaResponsetDTO> response = restTemplate.postForEntity(url, request, CompetenciaAbertaResponsetDTO.class);

Error:

org.springframework.web.client.RestClientException: Error while extracting response for type [class br.com.preventsenior.contasmedicas.infra.rest.client.dto.CompetenciaAbertaResponsetDTO] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Expected array or string.; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string.

I believe it is necessary to create a serialization class. But I don't know how to do it with a List of LocalDate.

Can you help me please

java

spring

spring-boot

rest

localdate

0 Answers

Your Answer

Accepted video resources