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"])
8
  • can you share input json or date field that is passed to this api Commented Aug 31, 2020 at 6: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. Commented Aug 31, 2020 at 6:54
  • this might be useful link Commented Aug 31, 2020 at 6:59
  • @rohithd - I have updated the question and added the generated JSON. Commented Aug 31, 2020 at 7:07
  • @Silhoutte - As per the link, I need to create a ContextResolver <ObjectMapper> 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 ? Commented Aug 31, 2020 at 7:21

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.