简体   繁体   中英

Use Provided jax-rs 2.0 and Jersey 2.1x in Weblogic 12c (12.2.1.3)

I need to implement a rest service call in my WEB application. According to Oracle, Weblogic is supported and does not need to register (deploy) jax-rs, so I would like to use these Server libraries. I made a simple class by calling a service (get). I configured the dependencies in the project and deployed it on Weblogic. However, when deploying, the following error appears: java.lang.ClassCastException: Cannot cast org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider to org.glassfish.jersey.server.spi.ComponentProvider

Note: It worked using this link below (deploying the jar on the server) But I want to use the native libraries on Weblogic. Could someone help me please?

https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297

Code example (Java)
        String host = "https://swapi.dev/api/people/2/";
        Client client = ClientBuilder.newBuilder().build();
        WebTarget webTarget = client.target(host);
        Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
        String result =  builder.get(String.class);


pom.xml

    <properties>
        <primefaces.version>3.5.RC1</primefaces.version>
        <jersey.version>2.21.1</jersey.version>
        <jaxrs.version>2.0</jaxrs.version>      
    </properties>
    <!-- JAX-RS -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- Jersey 2.21.1 -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
        <scope>provided</scope>
    </dependency>   
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
    </dependency>



 weblogic.xml

    <wls:weblogic-version>12.2.1.3</wls:weblogic-version>
    
    <wls:context-root>RecebimentoMercadoriaWEB</wls:context-root>

    <wls:library-ref>
        <wls:library-name>jsf</wls:library-name>
    </wls:library-ref>

    <wls:container-descriptor>
        <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
    </wls:container-descriptor>
    
    <wls:session-descriptor>
        <wls:cookie-name>CookieRecebimentoMercadoria</wls:cookie-name>
    </wls:session-descriptor>
    
    <wls:container-descriptor>
        <wls:prefer-application-packages>
            <wls:package-name>javax.faces.*</wls:package-name>
            <wls:package-name>com.sun.faces.*</wls:package-name>
            <wls:package-name>com.sun.facelets.*</wls:package-name>
            <wls:package-name>com.bea.faces.*</wls:package-name>
        </wls:prefer-application-packages>

        <wls:prefer-application-resources>
            <wls:resource-name>javax.faces.*</wls:resource-name>
            <wls:resource-name>com.sun.faces.*</wls:resource-name>
            <wls:resource-name>com.sun.facelets.*</wls:resource-name>
            <wls:resource-name>com.bea.faces.*</wls:resource-name>
            <wls:resource-name>META-INF/services/javax.servlet.ServletContainerInitializer</wls:resource-name>
            <wls:resource-name>META-INF/services/com.sun.faces.spi.FacesConfigResourceProvider</wls:resource-name>
        </wls:prefer-application-resources>
    </wls:container-descriptor>

On Weblogic 12.2.1.3 yo do not need to execute the procedure described by the link you have pointed, I mean the link below.

https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297

This is because that link belongs to Oracle Weblogic 12.1.3 and there are several differences between Weblogic 12.1.3.0 and Weblogic 12.2.1.3.

Furthermore, this document for Oracle Weblogic 12.2.1.3 states.

Note:

Jersey 2.x (JAX-RS 2.0 RI) support is provided by default in this release of WebLogic Server. Registration as a shared library is no longer required .

This means, when it comes to Weblogic 12.2.1.3 Jersey libraries are in place and ready to be used. Thus, your application should be able to use them.

However, I think server libraries are getting troubles with the libraries you are using within your pom.xml file.

Furthermore Oracle Weblogic 12.2.1.3 provides jersey 2.22.4

I have also used wls-cat in one of my servers to know, which library is loading the class org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider and I have found the library in $ORACLE_HOME/oracle_common/modules/org.glassfish.jersey.ext.cdi.jersey-cdi1x.jar , which means it is loaded by Weblogic as is stated on above documentation.

Furthermore, after running wls-cat I can see this:

org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider implements org.glassfish.jersey.server.spi.ComponentProvider

Thus, cast should not be an issue, which means there is a class loading problem that most probably is caused by libraries included in your application.

You can see the results of wls-cat executed on my server on below picture

在此处输入图像描述

You can use wls-cat to see which file (a JAR library) is loading the conflicting class. In below post you will find information about how to use wls-cat to analyse class loading problems.

https://blog.sysco.no/class/loader/AnalysingClassLoadingConflicts/

The libraries were really conflicting. I removed these dependencies from pom.xml and it worked. Thanks for the tip.

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
   
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>

   <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
        <scope>provided</scope>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM