4

I have 2 microservices which communicate via REST call. I have an Entity named Consumer which has various fields including a LocalDate. When i pass this entity via REST call, i get the following exception

Json parse error expected array or string.,nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException

In Entity class,i annotated like below

@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate dateOfBirth

In application.properties, i added the below line,

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

I am using Spring Boot version 2.1.2.RELEASE. In pom.xml, i have jackson dependencies.

I have added jackson-databind and jackson-datatype-jsr310,
both versions 2.9.9

I am using Retrofit as the client in 1st microservice through which i make the REST call to the REST ENDPOINT(@RestController) of the 2nd microservice.

But i get the error - Json Parse Error - MismatchedInputException for LocalDate. Is there anything more i need to add ?

EDIT-1 Following is the snippet of the generated JSON,

{"consumerId":1,"consumerName":"Harry","dateOfBirth":{"year":1991,"month":3,"day":10},"requestDate":"year":2020,"month":8,"day":31}

EDIT-2 I implemented following this link, http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/

Additionally added below as suggested by @rohit in the comments.

@JsonFormat(pattern="yyyy-MM-dd")
@JsonSerialize(using=LocalDateSerializer.class)
@JsonDeserialize(using=LocalDateDeserializer.class)
private LocalDate dateOfBirth

But still the date format generated in the JSON is not changing as per the format.

"dateOfBirth":{"year":1991,"month":3,"day":10}

It should be,

"dateOfBirth":"1991-03-10"

Isnt it ??

Is the bean defined in the SpringBoot main class not being used ?,

@Bean
@Primary
public ObjectMapper serializingObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer());
    javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
    objectMapper.registerModule(javaTimeModule);
    return objectMapper;
}

Now i am getting below error,

Json parse error: text; nested exception is com.fasterxml.jackson.databind.JsonMappingException: text (through reference chain com.model.Consumer["dateOfBirth"])
VinodKhade
  • 147
  • 1
  • 2
  • 10
  • can you share input json or date field that is passed to this api – rohithd Aug 31 '20 at 06:39
  • *"Json parse error **expected array or string**"* means that the actual JSON value is **not a string**, i.e. is not text formatted as `yyyy-MM-dd`. Check your assumptions, i.e. what the JSON value of property `dateOfBirth` actually is. – Andreas Aug 31 '20 at 06:54
  • this might be useful [link](https://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format) – Silhoutte Aug 31 '20 at 06:59
  • @rohithd - I have updated the question and added the generated JSON. – VinodKhade Aug 31 '20 at 07:07
  • @Silhoutte - As per the link, I need to create a ContextResolver bean in my SpringBoot Main class. ?. Also how to use the ObjectMapper in the RestController ? And do i have to format the Date using DateTimeFormatter before storing it in my Entity Object ? – VinodKhade Aug 31 '20 at 07:21
  • if you are passing the jSON you need to fromat to date type required and as expected in the api that is being consumed try adding @JsonFormat(pattern = "MM-dd-yyyy") @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) in your entity class – rohithd Aug 31 '20 at 08:18
  • @rohithd - I have updated the question after making the changes. But still getting error. I think it is not using the defined Object mapper ? – VinodKhade Aug 31 '20 at 09:26
  • Consumer model is at receiving request ? In Consumer model also follow the same . refer link below https://javarevisited.blogspot.com/2018/02/how-to-parse-json-with-date-field-in-java-jackson-example.html#:~:text=In%20order%20to%20correct%20deserialize,a%20date%20field%20in%20JSON. – rohithd Aug 31 '20 at 09:53

1 Answers1

1

As i was using Retrofit,

Added a registerTypeAdapter to my GsonBuilder as below,

gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer());
gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateDeserializer());
VinodKhade
  • 147
  • 1
  • 2
  • 10