1

I have a form where I use a field LocalDate that receives the value from a form mapped through Jackson. While JSON is:

Tue Jun 27 2017 00:00:00 GMT+0200 (ora legale Europa occidentale)   

My LocalDate variable is 2017-06-26 This is my pojo:

public class BookingForm {
    private Integer building;
    private Integer city;
    private LocalDate date;
    private String description;
    private LocalTime endTime;
    private List<Integer> externalUsers;
    private List<Integer> internalUsers;
    private String name;
    private Room room;
    private LocalTime startTime;
    private List<ExternalUser> newExternalUsers;
    //get and set method

And I have introduced Jackson for JDK8:

<!-- Jackson dependencies -->
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-hibernate5</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jacksonjsr310.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-parameter-names</artifactId>
    <version>${jacksondatatype.version}</version>
</dependency>

With Date instead of LocalDate all work fine.
Is there a problem with timezone or what else?

UPDATE: This is my Spring configuration:

public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
    MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    //authomatic registration
    mapper.findAndRegisterModules();

    messageConverter.setObjectMapper(mapper);
    return messageConverter;

}

is it possible to configure here?

luca
  • 3,248
  • 10
  • 66
  • 145

2 Answers2

0

I think your Jackson is set to use GMT and your BookingForm may be using something other like BST which is causing this

amittn
  • 2,249
  • 1
  • 12
  • 19
  • 2
    While this might answer the question, providing a working code example is much better than just pointing out the problem. You can find more tips in the [answer] page. – ItamarG3 Jun 26 '17 at 15:21
0

You need to add the JavaTimeModule with a deserializer set, and this deserializer must have a DateTimeFormatter (to parse the input).

For this test, I created a sample class:

public class JsonLocalDate {
    private LocalDate date;

    // getter/setter
}

And then:

String json = "{ \"date\": \"Tue Jun 27 2017 00:00:00 GMT+0200\" }";

ObjectMapper mapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// create formatter for the pattern
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    // append pattern
    .appendPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z")
    // create formatter - use English Locale to parse weekday and month name
    .toFormatter(Locale.ENGLISH);
// add the LocalDateDeserializer with the custom formatter
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter));
mapper.registerModule(module);

JsonLocalDate value = mapper.readValue(json, JsonLocalDate.class);
System.out.println(value.getDate());

The output is:

2017-06-27

  • Hi, I updated with my spring configuration, do you think it's possibile to set through that method? – luca Jul 04 '17 at 10:55
  • I'm not sure how to do a custom configuration along with `findAndRegisterModules`. Maybe you'll have to set it separately. –  Jul 04 '17 at 11:47