2

I am facing an issue with customised date formatting for JSON, where it of-course works in tests but fails on the deployed application. I want to use date pattern as dd-MM-yyyy pretty much standard and how it is expected here in India. There is a date formatter configured as well, and injected in the configuration like so

@Configuration
public class RepositoryWebConfiguration extends RepositoryRestMvcConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryWebConfiguration.class);

  @Override
  public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
    LOGGER
    .debug("Configuring dd-MM-yyyy as default date pattern for all JSON representations in Rest DATA Repositories");
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
    objectMapper = builder.build();
  }
}

Now this should work for JSON since I am injecting a specific date formatting, in my tests I first create a mapper with the same format

  private ObjectMapper halObjectMapper() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
    ObjectMapper objectMapper = builder.build();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.registerModule(new Jackson2HalModule());
    return objectMapper;
  }

I then use this mapper to generate the JSON for POST request. The JSON is generated all fine, I expected the format dd-MM-yyyy and I get exactly that

{
  "id":null,
  "name":"KABADI",
  "seatsAvailable":40,
  "workshopType":"KABADI FOUNDATION",
  "date":"16-08-2015",
  "venue":"http://localhost:8080/venues/2"
}

With the ObjectMapper registered I expect this JSON to be transformed to Workshop object without any issues & with the date format dd-MM-yyyy. However, the POST request fails due to format exception, and Jackson complains it cannot transform dd-MM-yyyy to Date as the available formats are "only"

("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")

here's the log

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '16-08-2015': not a valid representation (error: Failed to parse Date value '16-08-2015': Can not parse date "16-08-2015": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: HttpInputOverHTTP@54153158; line: 5, column: 53] (through reference chain: com.agilityroots.doi.workshop.entity.Workshop["date"])
  at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
  at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:810)
  at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:740)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:176)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:262)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:246)
  at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
  at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:238)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
  ... 46 common frames omitted

never had to look too much into these overrides earlier as in the usual Spring WebMVC + Boot scenario, this property used to the do the trick

spring.jackson.date-format=dd-MM-yyyy

So I might as well be missing something here, or I am configuring objectMapper in the wrong way in the sense that it is not injected? How can I get JSON transformers to accept dd-MM-yyyy format?

Anadi Misra
  • 1,925
  • 4
  • 39
  • 68
  • Are you sure that the `ObjectMapper` you've customized is actually being used for de-serialization? – Mick Mnemonic Aug 16 '15 at 06:51
  • I am not, and well, did what the documentation said, to use one of the `configureXXX` methods to override configuration, hence, implemented the `configureJacksonObjectMapper` method. I expect it to be used but looks like it isn't. The question is why? – Anadi Misra Aug 16 '15 at 07:02

1 Answers1

0

Was doing too much, this fixed the test for now. Object to JSON is still not in dd-MM-yyyy format, gets interpreted as a separate date for example 16-08-2015 returns as February X in 22 will look into that

  @Override
  public void configureJacksonObjectMapper(ObjectMapper objectMapper) {

    objectMapper.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
  }

Update:

The other problem was annotation the date field in Workshop with @Temporal(TemporalType.DATE) which caused the date to be saved as 2015-08-16, removed that annotation too.

Anadi Misra
  • 1,925
  • 4
  • 39
  • 68