Skip to content

flistell/JsonbDateFormat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jackson JsonFormat with JavaEE 8 JAX-RS/Jersey

Demo code to show how to force the use of Jackson 2.x with Jersesy 2.x.

Description

The repository contains 2 Web applications that implements a simple REST service with a single resource "/datedemo". The resource has a "Date" field customized with Jackson @JsonFormat annotation. This resource would return a JSON object like this:

{
  "date": "30/06/2023",
  "calendar": 1688139526184,
  "gregorianCalendar": 1688139526184,
  "timeZone": "Europe/Rome"
}

Note: following result would be WRONG and means that JacksonFeature is not loaded and @JsonFormat annotation is not working, and this was happening intermittently on app restart:

{
  "date": 1688139881752,
  "calendar": 1688139881752,
  "gregorianCalendar": 1688139881752,
  "timeZone": "Europe/Rome"
}

The code here demostrate how setting the property jersey.config.jsonFeature to JacksonJsonProvider, would always force the use of Jackson over Yasson and makes @JsonFormat to always work.

recordrest-app-jackson

This application sets jersey.config.jsonFeature programmatically in JaxRsWithJacksonResourceConfig, a Jersey configuration class that extends org.glassfish.jersey.server.ResourceConfig:

        register(JacksonFeature.class);
        register(JacksonJsonProvider.class);
        register(DateDemoService.class);
        property("jersey.config.jsonFeature", "JacksonJsonProvider");

Test endpoint is: http://{{SERVER}}:{{PORT}}/recordrest-app-jackson/resources2/datedemo

recordrest-web-jackson

This application sets jersey.config.jsonFeature in web.xml while defying the jax-rs service configuration:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <servlet>
        <servlet-name>JaxRsWithJacksonWebApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>it.fl.poc.jsondatejackson.rest</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.jackson.JacksonFeature,com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.jsonFeature</param-name>
            <param-value>JacksonJsonProvider</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>JaxRsWithJacksonWebApplication</servlet-name>
        <url-pattern>/resources3/*</url-pattern>
    </servlet-mapping>
</web-app>

Test endpoint is: http://{{SERVER}}:{{PORT}}/recordrest-web-jackson/resources3/datedemo

Build

Build with usual

    $ mvn package

Resources

Source repos

Specs

Isseus

WebLogic

Other