0

I have a REST endpoint in a container (Payara 4)
I have added the dependency to the pom.xml for the joda serialization extension.

<dependency>
     <groupId>com.fasterxml.jackson.datatype</groupId>
     <artifactId>jackson-datatype-joda</artifactId>
     <version>2.10.3</version>
</dependency>

But I get the items serialized incorrectly.

"dt":{"chronology":{},"millis":1499896800000}

I know I need to use jackson-databind-joda, but I am not sure how to register it correctly in PAYARA. There are examples for Spring Boot, but not for the older application servers?

@Path("bom")
@Stateless
public class ProductionMaterialRestEndpoint {

    @POST
    @Path("kw/compare/{year}/{week}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public ResultContainingDateTime compare(
            @PathParam("year") int year,
            @PathParam("week") int week) throws IOException, ExecutionException {

            ...               
    }    
}


public class ResultContainingDataTime {
    private org.joda.time.DateTime dt;
}

how do I register the joda serializer module with the serializer built into PAYARA /GLASSFISH

Jim
  • 14,952
  • 15
  • 80
  • 167
  • which Java EE version are you using? Payara or Glassfish don't use Jackson out-of-the-box. Did you manually configure Jackson to be used as the message body writer/reader for JSON payload? – rieckpil Apr 28 '20 at 08:02
  • we are using payara 4.1.2.181 – Jim Apr 28 '20 at 12:26
  • I think you might be on to something, I think it's using JAX-RS and not jackson, so how would I fix the serialization then? – Jim Apr 28 '20 at 12:30

1 Answers1

0

JAX-RS is basically the specification on how to write REST web services in Java EE. It does not specify which technology is used to serialize messages but only the interface MessageBodyReader and MessageBodyReader.

If you use Java EE 8, you'll get JSON-B (Binding) that ensures JSON serialization out-of-the-box. If you want to use Jackson instead, you need to configure it (How to use Jackson 2 in Payara 5?). Furthermore, you don't get the default auto-configuration of Jackson modules (e.g. Java Time, Joda Time) out-of-the-box like for Spring Boot and have to register them for your ObjectMapper by yourself.

If your code is using Java 8 and Java EE 8, you can also use the java.time classes like LocalDateTime, Instant, etc. and use JSON-B annotations to specify how you want to serialize these values using @JsonbDateFormat

rieckpil
  • 10,470
  • 3
  • 32
  • 56
  • do you perhaps know how I would register them with the `ObjectMapper` so that all JAX-RS responses use the configured mapper? – Jim Apr 29 '20 at 06:20