Demo code to show how to force the use of Jackson 2.x with Jersesy 2.x.
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.
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
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 with usual
$ mvn package
- eclipse-ee4j/jersey at 2.29
- rest/jaxrs-api at 2.1.6 · jakartaee/rest · GitHub
- FasterXML/jackson: Main Portal page for the Jackson project
- What's New in Oracle WebLogic Server
- Developing and Securing RESTful Web Services for Oracle WebLogic Server
- Developing JAX-WS Web Services for Oracle WebLogic Server
- Upgrading Oracle WebLogic Server
- Java API for JSON Processing
- Java API for JSON Binding
- Code search results FEATURE_AUTO_DISCOVERY· GitHub
- Configuring JAX-RS web applications - IBM Documentation
- FasterXML/jackson-jaxrs-providers: Multi-module project that contains Jackson-based "old" JAX-RS (ones under `javax.ws.rs`) providers for JSON, XML, YAML, Smile, CBOR formats
- java - How to disable Jersey's JacksonJsonProvider auto registration so I use my own? - Stack Overflow ⭐⭐⭐⭐
- java - How to set up JAX-RS Application using annotations only (no web.xml)? - Stack Overflow
- java - Jersey: disable default JSON provider - Stack Overflow ⭐⭐⭐⭐⭐
- java - Use Jackson as JAXB-JSON-processor in JavaEE Application - Stack Overflow
- jax rs - What exactly is the ResourceConfig class in Jersey 2? - Stack Overflow
- JAX-RS 2.0 behavior changes - IBM Documentation
- Jersey 2.6 Jackson provider registering - Stack Overflow
- json - Unable to upgrade to Jackson 2.1.4, Jersey ignoring the annotations - Stack Overflow
- rest - Configure Jackson as JSON Provider in JAX-RS 2.0 - Stack Overflow
- rest - Do we still need JacksonFeature.class for Jersey 2.17 projects? - Stack Overflow
- Using Jackson as JSON provider in Jersey 2.x | cassiomolin