2

I would like to change the JSON parser from Moxy to Jackson, and I have followed the shared library approach, but without any result, Moxy is still used.

Note: the shared library is not necessary!!!

The shared library

I have created a maven project with the following dependencies and descriptors.

pom.xml

 <properties>
    <jackson.annotations.release>2.8.6</jackson.annotations.release>
    <jersey.version>2.22.4</jersey.version>
 </properties>

 <dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jaxb-annotations</artifactId>
        <version>${jackson.annotations.release}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.annotations.release}</version>
    </dependency>

    <!-- Jersey dependencies -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependency>

weblogic.xml

 <container-descriptor>
    <prefer-application-packages>
        <!-- apis -->
        <package-name>javax.ws.rs.*</package-name>

        <!-- guava -->
        <package-name>com.google.common.*</package-name>

        <!-- jersey providers -->
        <package-name>org.glassfish.jersey.*</package-name>
        <package-name>jersey.repackaged.*</package-name>

        <!-- hk2 -->
        <package-name>org.jvnet.hk2.*</package-name>
        <package-name>org.jvnet.hk2.tiger_types.*</package-name>
        <package-name>org.glassfish.hk2.*</package-name>
        <package-name>javassist.*</package-name>

        <!-- media providers -->
        <!--<package-name>org.eclipse.persistence.jaxb.rs.*</package-name>-->
        <package-name>com.fasterxml.jackson.*</package-name>

        <!-- wls -->
        <package-name>weblogic.jaxrs.api.client.*</package-name>
        <package-name>weblogic.jaxrs.internal.api.client.*</package-name>
        <package-name>weblogic.jaxrs.dispatch.*</package-name>
        <package-name>weblogic.jaxrs.monitoring.util.*</package-name>
    </prefer-application-packages>
</container-descriptor>

MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: Jackson Weblogic
Implementation-Version: 1.0
Specification-Title: Jackson Weblogic
Extension-Name: jackson-weblogic
Specification-Version: 1.0

Web Application

I add the web.xml and the weblogic.xml.

web.xml

<servlet>
    <servlet-name>com.dummy.MyApplication</servlet-name>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider</param-value>
        </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>com.dummy.MyApplication</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
    <library-ref>
        <library-name>jstl</library-name>
        <specification-version>1.2</specification-version>
        <implementation-version>1.2</implementation-version>
        <exact-match>false</exact-match>
    </library-ref>
    <library-ref>
        <library-name>jackson-weblogic</library-name>
        <specification-version>1.0</specification-version>
        <exact-match>false</exact-match>
    </library-ref>
</weblogic-web-app>

Then I reference in the weblogic.xml the shared library, but Moxy is still used. Have I done something wrong?

Update: I have added the web.xml and the weblogic.xml of the web application.

Solution

I have checked the folder oracle_common\modules and I have found all the Jackson dependencies needed. So I have preferred to configure Jersey to disable the use of MOXy as follows:

import javax.ws.rs.core.Application;
public class MyApplication extends Application
{
    @Override
    public Map<String, Object> getProperties()
    {
        Map<String, Object> proprties = new HashMap<>();
        proprties.put("jersey.config.server.disableMoxyJson", true);

        return proprties;
    }
}

Pay attention:

  • that the method getProperties() comes with javax.ws.rs 2.0;
  • no specific shared library is necessary for Jackson, but just for jstl and jax-rs 2.0;
  • no info inside web.xml is required;
  • as reported by @abbas JAX-RS 2.0 comes by default with Weblogic 12.2.1.3.0 in my case I has a transitive dependency on javax.ws.rs:jsr311-api so the method getProperties() was not invoked.

Final weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">

    <context-root>/web-app</context-root>
    <container-descriptor>
        <prefer-application-packages>
            <package-name>org.apache.commons.net.*</package-name>
        </prefer-application-packages>
    </container-descriptor>

    <library-ref>
        <library-name>jstl</library-name>
        <specification-version>1.2</specification-version>
        <implementation-version>1.2</implementation-version>
        <exact-match>false</exact-match>
    </library-ref>
</weblogic-web-app>
  • If both are on the classpath, MOXy will always win out, unless you explicitly register the `JacksonFeature`. – Paul Samsotha Oct 18 '17 at 21:59
  • @peeskillet do you mean what I have added in the `web.xml`? or is it better to register the feature in a different way? Jax-rs 2.x uses SPI-based auto-registration so I have though it was not necessary the `web.xml` but even with it `weblogic` still uses `moxy`. Thanks for your time ;-) – felix at housecat Oct 19 '17 at 06:25
  • @peeskillet thanks again, I think I have found another solution, I have tested in WildFly as well and it seems there are no drawbacks so far. – felix at housecat Oct 19 '17 at 10:42
  • at least you do not need to library-ref for JAX-RS 2.0 because it is now part of Weblogic 12.2.1 – abbas Nov 01 '17 at 06:29
  • @abbas the lib JAX-RS 2.0 is under the folder `wlserver\common\deployable-libraries` and as far as I have understood it has to be deployed otherwise the method `getProperties()`, part of specification 2.0, is not called. I suspect that Weblogic by default uses the old library. – felix at housecat Nov 01 '17 at 08:31
  • Check this sample project which I created last night https://github.com/esafzay/jax-rs-2-java-ee-6-weblogic-12.2.1 – abbas Nov 01 '17 at 09:12
  • @abbas thanks a lot, you are right, it is not necessary to deploy the shared library JAX-RS 2.0, the issue on my side was to due to `swagger` having a dependency on `jsr311` and so loading wrong version, once I removed it everything was fine. – felix at housecat Nov 01 '17 at 12:40

1 Answers1

3

Mention JAX-RS 2.0 as a dependency in your pom.xml

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
</dependency>

In WebLogic 12.2.1 you do not need to deploy it as a shared library.

abbas
  • 6,453
  • 2
  • 40
  • 36