Giter Club home page Giter Club logo

jsonbdateformat's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.