使用提供的jax-rs 2.0和Jersey 2.1x在Weblogic 12c(12.2.1.3)中。

huangapple go评论93阅读模式
英文:

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

问题

我需要在我的WEB应用程序中实现一个REST服务调用。根据Oracle的文档,Weblogic支持并且不需要注册(部署)jax-rs,因此我想使用这些服务器库。我创建了一个简单的类来调用一个服务(get方法)。我在项目中配置了依赖项并部署到了Weblogic上。然而,在部署时出现了以下错误:java.lang.ClassCastException: 无法将org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider转换为org.glassfish.jersey.server.spi.ComponentProvider

注意:在以下链接中运行良好(在服务器上部署jar文件)。但我想在Weblogic上使用本地库。有人能帮帮我吗?

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

代码示例(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>
<!-- 其他 Jersey 依赖 -->

weblogic.xml文件:

<wls:weblogic-version>12.2.1.3</wls:weblogic-version>
<wls:context-root>RecebimentoMercadoriaWEB</wls:context-root>
<!-- 其他配置 -->

请注意,我已经对代码和配置进行了翻译。如果您有任何其他问题,请随时提问。

英文:

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 = &quot;https://swapi.dev/api/people/2/&quot;;
		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

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



 weblogic.xml

 	&lt;wls:weblogic-version&gt;12.2.1.3&lt;/wls:weblogic-version&gt;
    
    &lt;wls:context-root&gt;RecebimentoMercadoriaWEB&lt;/wls:context-root&gt;

	&lt;wls:library-ref&gt;
		&lt;wls:library-name&gt;jsf&lt;/wls:library-name&gt;
	&lt;/wls:library-ref&gt;

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

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

答案1

得分: 4

在Weblogic 12.2.1.3上,您不需要执行您指向的链接中描述的过程,我指的是下面的链接。

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

这是因为该链接属于Oracle Weblogic 12.1.3,而Weblogic 12.1.3.0与Weblogic 12.2.1.3之间存在几个差异。

此外,这份文档适用于Oracle Weblogic 12.2.1.3

> 注意:
>
> 在此版本的WebLogic Server中,默认提供了Jersey 2.x(JAX-RS 2.0 RI)支持不再需要将其注册为共享库。

这意味着,对于Weblogic 12.2.1.3,Jersey库已经就位,可以随时使用。因此,您的应用程序应该能够使用它们。

然而,我认为服务器库与您在pom.xml文件中使用的库存在冲突。

此外,Oracle Weblogic 12.2.1.3提供了jersey 2.22.4

我还在我的一个服务器上使用了wls-cat来查看哪个库加载了类org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider,我发现该库位于**$ORACLE_HOME/oracle_common/modules/org.glassfish.jersey.ext.cdi.jersey-cdi1x.jar**,这意味着它是由Weblogic加载的,正如上面的文档所述。

此外,在运行wls-cat后,我可以看到以下信息:

org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider实现了org.glassfish.jersey.server.spi.ComponentProvider

因此,类型转换应该不是问题,这意味着存在一个类加载问题,很可能是由您的应用程序中包含的库引起的。

您可以使用wls-cat查看加载冲突类的哪个文件(JAR库)。在下面的帖子中,您将找到有关如何使用wls-cat分析类加载问题的信息。

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

英文:

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

使用提供的jax-rs 2.0和Jersey 2.1x在Weblogic 12c(12.2.1.3)中。

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/

答案2

得分: 3

这些库之间存在冲突。我从pom.xml中移除了这些依赖项,然后它就正常工作了。感谢您的建议。

<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 libraries were really conflicting. I removed these dependencies from pom.xml and it worked. Thanks for the tip.

    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
        &lt;version&gt;${jersey.version}&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;
   
	&lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
        &lt;version&gt;${jersey.version}&lt;/version&gt;
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt;

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

huangapple
  • 本文由 发表于 2020年8月5日 01:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63252526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定