How to configure Jackson in Wildfly?
"How can I configure Jackson and its SerializationFeature in Wildfly?"
You don't need to configure it in Wildfly, you can configure it in the JAX-RS applciation. Just use a ContextResolver
to configure the ObjectMapper
(see more here). Something like
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}
If you don't already have the Jackson dependency, you need that, just as a compile-time dependency
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.8.Final</version>
<scope>provided</scope>
</dependency>
If you are using scanning to discover your resource classes and provider classes, the ContextResolver
should be discovered automatically. If you explicitly registering all your resource and providers, then you'll need to register this one also. It should be registered as a singleton.
UPDATE
As @KozProv mentions in a comment, it should actually be resteasy-jackson2-provider
as the artifactId for the Maven dependency. -jackson-
uses the older org.codehaus
(Jackson 1.x), while the -jackson2-
uses the new com.fasterxml
(Jackson 2.x). Wildfly by default uses The Jackson 2 version.
Configure Wildfly 10 to use Jackson (as JSON provider)
The fix was to put the exclusions and the dependencies in the sub-deployment, instead of deployment tag, as I was doing.
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<sub-deployment name="axis.war">
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson2-provider"/>
<module name="org.jboss.resteasy.resteasy-jettison-provider"/>
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson-provider" services="import"/>
</dependencies>
</sub-deployment>
How do I get Wildfly to use additional Jackson Datatypes?
Ultimately I was affected by This Bug in 8.0.0.Final it should be fixed in 8.0.1, until then I've downgraded to 8.0.0.CR1.
Here's my Final pom.xml
, note that I'm using 2.2.3
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.2.3-beta5</version>
</dependency>
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
...
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.lm.infrastructure.JacksonProducer</param-value>
</context-param>
</web-app>
my jboss-deployment-structure.xml
this is required, and I didn't have it previously
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider" />
<module name="org.jboss.resteasy.resteasy-jettison-provider" />
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import" />
</dependencies>
</deployment>
</jboss-deployment-structure>
My Producer
package com.lm.infrastructure;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import com.fasterxml.jackson.databind.ObjectMapper;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
@Provider
@Produces( MediaType.APPLICATION_JSON )
public class JacksonProducer implements ContextResolver<ObjectMapper> {
public JacksonProducer() throws Exception {
this.json
= new ObjectMapper()
.findAndRegisterModules()
.configure( WRITE_DATES_AS_TIMESTAMPS, false )
.configure( FAIL_ON_UNKNOWN_PROPERTIES, false );
}
@Override
public ObjectMapper getContext( Class<?> objectType ) {
return json;
}
private final ObjectMapper json;
}
and last but not least my arquillian war generation
public static WebArchive testWar() {
File[] libs
= Maven.resolver().loadPomFromFile( "pom.xml" )
.resolve(
"com.fasterxml.jackson.datatype:jackson-datatype-jsr310"
)
.withTransitivity()
.asFile();
return ShrinkWrap.create( WebArchive.class )
.setWebXML( new File( "src/main/webapp/WEB-INF/web.xml" ) )
.addAsWebInfResource( new File( "src/main/webapp/WEB-INF/jboss-web.xml" ) )
.addAsWebInfResource( new File( "src/main/resources/logback.xml" ) )
.addAsWebInfResource( new File( "src/main/webapp/WEB-INF/jboss-deployment-structure.xml" ) )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" )
.addPackages( false, Filters.exclude( ".*Test.*" ), getCorePackages() )
.addAsLibraries( libs );
}
Wildfly 10 and Jackson 2
Regarding this error message:
java.lang.NoClassDefFoundError: Failed to link
com/fasterxml/jackson/jaxrs/base/ProviderBase
or:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/jaxrs/base/ProviderBase
WildFly is not loading/exporting all the needed libraries. Your jboss-deployment-structure.xml file should look something like this:
<deployment>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
<!-- This module contain the ProviderBase class: -->
<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" export="true"/>
</dependencies>
<exclusions>
<!-- Just to make sure these modules are not loaded -->
<module name="org.jboss.resteasy.resteasy-jackson-provider"/>
<module name="org.jboss.resteasy.resteasy-jettison-provider"/>
</exclusions>
</deployment>
WIldfly | Jackson polymorphism not considered
I've filed RESTEASY-2948 as this is not documented properly. To fix this you need to set the resteasy-prefer-jackson-over-jsonb
property. The simplest way is to use CLI to change the value in the subsystem:
/subsystem=jaxrs:write-attribute(name=resteasy-prefer-jackson-over-jsonb, value=true)
This should also not require you to include a jboss-deployment-structure.xml
. One thing to note though is this does affect all deployments.
using Jackson annotations in Wildfly
The RESTEasy and Jackson dependencies should be marked as <scope>provided</scope>
.
Also if you're only using JAX-RS and Jackson in your WAR, just move jboss-deployment-structure.xml
to your WAR/WEB-INF
directory. If you don't want to move it you might need to add a <sub-deployment/>
.
<jboss-deployment-structure>
<sub-deployment name="rest-module.war">
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider"/>
</exclusions>
<dependencies>
<module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
</dependencies>
</sub-deployment>
</jboss-deployment-structure>
Error com.fasterxml.jackson Deploy .WAR Spring Boot in JBoss/Wildfly
- Create file named jboss-deployment-structure.xml
- Add the below block
- Copy file in location WEB-INF/jboss-deployment-structure.xml
<?xml version='1.0' encoding='UTF-8'?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
<exclusions>
<module name="com.fasterxml.jackson.core.jackson-annotations" />
<module name="com.fasterxml.jackson.core.jackson-core" />
<module name="com.fasterxml.jackson.core.jackson-databind" />
<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
<module name="org.jboss.resteasy.resteasy-jackson2-provider" />
<module name="org.slf4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>
IMG - Preview project Maven
Spring boot jackson auto configuration linkage error wildfly
After investigating more jboss classloading behavior, jackson classes inside B.jar are conflicting with same jackson classes from jboss restesay module.
so after excluding them in jboss-deployment-structure.xml as below this problem is fixed..
<exclusions>
<module name="org.jboss.resteasy.resteasy-jackson-provider"/>
<module name="org.jboss.resteasy.resteasy-jackson2-provider"/>
</exclusions>
Related Topics
How Does Java Resolve a Relative Path in New File()
Resizing Icon to Fit on Jbutton in Java
Building Executable Jar with Maven
Disable a Particular Checkstyle Rule for a Particular Line of Code
How to Add a Maven Dependency in Eclipse
How Can a Class Have a Member of Its Own Type, Isn't This Infinite Recursion
How to Call the Overridden Method of a Superclass
Why Is T Bounded by Object in the Collections.Max() Signature
How to Get Data Between Quotes in Java
The Method Getdispatchertype() Is Undefined for the Type Httpservletrequest
How to Convert a 3D Point into 2D Perspective Projection
How to Split String with Some Separator But Without Removing That Separator in Java
How to Check CPU and Memory Usage in Java
Why Does Double.Nan==Double.Nan Return False
How to Check That a Java String Is Not All Whitespaces
Java Regex Replace with Capturing Group
Jtable Disable Checkbox in Cell
Jpa: How to Have One-To-Many Relation of the Same Entity Type