1

I am implementing REST service(POST method) using jersey and I have LocalDateTime field in request object. When I send json request using postman, LocalDateTime field is being set as null in request object.

I tested serialization by setting the LocalDateTime value and sending it in the response. it works fine.

Below is my code, pom.xml, sample request and response. Can someone please help to resolve this?

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

public class Request {
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime startDateTime;

@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime endDateTime;

private String id;
}

//REST API

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response test(Request request) {
    request.setStartDateTime(LocalDateTime.now());
    return Response.ok().entity(request).build();
}

//ContextResolver

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper MAPPER;

public ObjectMapperContextResolver() {
    MAPPER = new ObjectMapper();
    MAPPER.registerModule(new JavaTimeModule());
    MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}

@Override
public ObjectMapper getContext(Class<?> type) {
    return MAPPER;
}
}

POM

<jersey.version>2.22.2</jersey.version>
 <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.25.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.9.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.9.7</version>
    </dependency>
</dependencies>

Request

{
 "startDateTime" : "2011-01-01T00:00:30.000",
 "endDateTime" : "2011-12-12T00:00:33.000",
 "id" : "testString"
}

Response

{
 "startDateTime" : "2018-11-21T11:50:58.560", //this is the value I am setting before sending response
 "id" : "testString"
}
Atul Kumbhar
  • 1,073
  • 16
  • 26
  • As mentioned in [this](https://stackoverflow.com/a/46323381/2909062) answer, I implemented the adapter class and package-info.java. And my issue seems to be resolved. Only one thing I did wrong was putting package-info.java in higher level package. package-info.java should be kept in same package as model. – Atul Kumbhar Nov 21 '18 at 04:16

0 Answers0