Search code examples
javamavenjacksonwildflywildfly-10

Wildfly 10 and Jackson 2


I have problem like Jackson 2 on Wildfly 10: How to load ProviderBase?

All dependecies in maven - provided

jboss-deployment-structure Is it right?

<deployment>
    <dependencies>
        <module name="by.lib" meta-inf="export" export="true"/>
        <module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
        <!--<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider"  export="true"/>-->
        <!--<module name="org.jboss.resteasy.resteasy-jaxrs"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-databind"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-core"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-annotations"  export="true"/>-->
        <!--<module name="org.jboss.resteasy.resteasy-jackson2-provider"  export="true"/>-->
    </dependencies>
    <exclude-subsystems>
        <subsystem name="logging"/>
        <subsystem name="resteasy"/>
    </exclude-subsystems>
</deployment>

In module I have dependecies on jackson-jaxrs-base-2.8.5

I catch different errors

13:10:27,005 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 59) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: Failed to link com/fasterxml/jackson/jaxrs/base/ProviderBase (Module "by.lib:main" from local module loader @1c2c22f3 (finder: local module finder @18e8568 (roots: D:\Work\Server_10.1.0\modules,D:\Work\Server_10.1.0\modules\system\layers\base))): javax/ws/rs/ext/MessageBodyReader
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: Failed to link com/fasterxml/jackson/jaxrs/base/ProviderBase (Module "by.lib:main" from local module loader @1c2c22f3 (finder: local module finder @18e8568 (roots: D:\Work\Server_10.1.0\modules,D:\Work\Server_10.1.0\modules\system\layers\base))): javax/ws/rs/ext/MessageBodyReader
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3937)

or

13:14:10,114 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 25) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: Failed to link com/fasterxml/jackson/jaxrs/base/ProviderBase (Module "by.lib:main" from local module loader @1c2c22f3 (finder: local module finder @18e8568 (roots: D:\Work\Server_10.1.0\modules,D:\Work\Server_10.1.0\modules\system\layers\base))): javax/ws/rs/ext/MessageBodyReader
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: Failed to link com/fasterxml/jackson/jaxrs/base/ProviderBase (Module "by.lib:main" from local module loader @1c2c22f3 (finder: local module finder @18e8568 (roots: D:\Work\Server_10.1.0\modules,D:\Work\Server_10.1.0\modules\system\layers\base))): javax/ws/rs/ext/MessageBodyReader
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3937)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3941)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4824)

When I try

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class ConfiguredRESTProvider implements ContextResolver<ObjectMapper> {

    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
        AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
        AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
        AnnotationIntrospector pair = AnnotationIntrospectorPair.create(primary, secondary);
        mapper.setAnnotationIntrospector(pair);

        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        // TODO Разбраться почему не отрабатывае? ("balanceAmount": 2.41E8)
        mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // mapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS,true);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    }

    // public void ConfiguredRESTProvider() {
    // // ObjectMapper mapper = new ObjectMapper();
    // ObjectMapper mapper = locateMapper(type, json);
    // ObjectMapper mapper = super._mapperConfig.getDefaultMapper();
    // // AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    // // AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
    // // AnnotationIntrospector pair = AnnotationIntrospectorPair.create(primary, secondary);
    // // mapper.setAnnotationIntrospector(pair);
    //
    // mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
    // // TODO Разбраться почему не отрабатывае? ("balanceAmount": 2.41E8)
    // mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    // mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    // mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // // mapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS,true);
    // mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    // super.setMapper(mapper);
    // }

    @Override
    public ObjectMapper getContext(Class<?> aClass) {
        return mapper;
    }

}

Jboss Structure

<deployment>
    <dependencies>
        <module name="by.lib" meta-inf="export" export="true" services="import"/>
        <module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
        <!--<module name="org.jboss.resteasy.resteasy-jackson2-provider" export="true"/>-->
        <!--<module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider"  export="true"/>-->
        <!--<module name="org.jboss.resteasy.resteasy-jaxrs"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-databind"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-core"  export="true"/>-->
        <!--<module name="com.fasterxml.jackson.core.jackson-annotations"  export="true"/>-->
        <!--<module name="org.jboss.resteasy.resteasy-jackson2-provider"  export="true"/>-->
    </dependencies>
    <exclusions>
        <module name="org.jboss.resteasy.resteasy-jackson-provider"/>
        <module name="org.jboss.resteasy.resteasy-jettison-provider"/>
        <module name="org.jboss.resteasy.resteasy-jackson2-provider"/>
    </exclusions>
    <exclude-subsystems>
        <subsystem name="logging"/>
        <!--<subsystem name="resteasy"/>-->
    </exclude-subsystems>
</deployment>

I have such error

2017-01-12 10:59:27,659 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 93) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/jaxb/JaxbAnnotationIntrospector
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructors(Class.java:1651)
    at org.jboss.resteasy.util.PickConstructor.pickSingletonConstructor(PickConstructor.java:30)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.createConstructorInjector(ResteasyProviderFactory.java:2252)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.createProviderInstance(ResteasyProviderFactory.java:2244)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.addContextResolver(ResteasyProviderFactory.java:1130)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.registerProvider(ResteasyProviderFactory.java:1667)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.registerProvider(ResteasyProviderFactory.java:1368)
    at org.jboss.resteasy.spi.ResteasyProviderFactory.registerProvider(ResteasyProviderFactory.java:1290)
    at org.jboss.resteasy.spi.ResteasyDeployment.registerProvider(ResteasyDeployment.java:571)
    at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:347)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:250)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:113)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
    at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.

Solution

  • 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>