java.lang.ClassNotFoundException: javax.ws.rs.MessageProcessingException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27868936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:33:44  来源:igfitidea点击:

java.lang.ClassNotFoundException: javax.ws.rs.MessageProcessingException

javatomcatcxfjax-rsjersey-client

提问by Daniel Kaplan

I am deploying a war to Tomcat 7.0.57. This code uses Jersey 2.x Client to communicate with a Rest endpoint and exposes its own Rest endpoints using CXF (AKA the war's endpoint).

我正在向 Tomcat 7.0.57 部署War。此代码使用 Jersey 2.x 客户端与 Rest 端点通信,并使用 CXF(也称为War端点)公开其自己的 Rest 端点。

When I hit one of the war's endpoints, the code seems to work and returns a response without issue from the server's perspective, but the client gets a 500 response returned to it from tomcat. This is the error:

当我击中War的一个端点时,代码似乎可以工作并从服务器的角度返回一个没有问题的响应,但客户端从 tomcat 收到一个 500 响应。这是错误:

java.lang.ClassNotFoundException: javax.ws.rs.MessageProcessingException
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    org.apache.cxf.jaxrs.impl.ResponseBuilderImpl.build(ResponseBuilderImpl.java:69)
    org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:137)
    org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:86)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
    org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:167)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:206)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Oddly enough, this error doesn't show up in the tomcat log.

奇怪的是,这个错误没有出现在 tomcat 日志中。

I did a little research and it seems that this class is part of jax-rs. In my maven pom, I'm already including this dependency:

我做了一些研究,看来这个类是 jax-rs 的一部分。在我的 maven pom 中,我已经包含了这个依赖项:

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

But this dependency doesn't seem to have this class. That class isin this dependency though:

但是这个依赖好像没有这个类。该类虽然在此依赖项中:

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0-m01</version>
    </dependency>

I feel uncomfortable downgrading to a lower version that's a milestone though. More importantly, that jar does not include classes I'm using in my code like javax.ws.rs.client.ClientBuilder.

降级到较低的版本让我感到不舒服,但这是一个里程碑。更重要的是,该 jar 不包含我在代码中使用的类,如javax.ws.rs.client.ClientBuilder.

Can anyone explain why I'm getting this exception and how to resolve this issue?

谁能解释为什么我会收到此异常以及如何解决此问题?



My Workaround

我的解决方法

I've come to the conclusion that this has more to do with the Jersey Client and CXF interfering with each other. I decided to remove the Jersey Client and replace it with the CXF Client. These instructions are much better than the official ones: http://fandry.blogspot.com/2012/06/how-to-create-simple-cxf-based-jax-rs.html

我得出的结论是,这更多地与 Jersey 客户端和 CXF 相互干扰有关。我决定删除 Jersey 客户端并用 CXF 客户端替换它。这些说明比官方的好很多:http: //fandry.blogspot.com/2012/06/how-to-create-simple-cxf-based-jax-rs.html

import com.fasterxml.Hymanson.jaxrs.json.HymansonJaxbJsonProvider;
import org.apache.cxf.jaxrs.client.WebClient;
import java.util.ArrayList;
import java.util.List;

public class App 
{
    public static void main( String[] args )  throws Exception { 

     List<Object> providers = new ArrayList<Object>();
     providers.add( new HymansonJaxbJsonProvider() );

     WebClient client = WebClient.create("http://localhost:8080/poc_restapi_cxf/api", providers);
     client = client.accept("application/json").type("application/json").path("/order").query("id", "1");

     Order order = client.get(Order.class);
     System.out.println("Order:" + order.getCustomerName());

    }
}

Very similar API. I just searched and replaced some method names and everything worked.

非常相似的API。我只是搜索并替换了一些方法名称,一切正常。

回答by Sam Keays

I came across this issue too. Unlike you project requirements meant I had to get Jersey and CXF playing nice.

我也遇到过这个问题。不像你的项目要求意味着我必须让 Jersey 和 CXF 玩得很好。

The ultimate problem is in the FactoryFinder class which is used by Jersey to get the appropriate runtime delegate (used for various subsidiary objects) - this in turn has a class with an import to the offending class.

最终的问题是在 FactoryFinder 类中,Jersey 使用它来获取适当的运行时委托(用于各种附属对象)——这又具有一个导入到违规类的类。

To get around this you need to have a file in your resource folder (presuming this gets imported to your classpath folder) with the path META-INF/services/javax.ws.rs.ext.RuntimeDelegate that contains the value:

为了解决这个问题,你需要在你的资源文件夹中有一个文件(假设它被导入到你的类路径文件夹),其路径 META-INF/services/javax.ws.rs.ext.RuntimeDelegate 包含值:

org.glassfish.jersey.internal.RuntimeDelegateImpl

This should solve your issue.

这应该可以解决您的问题。

回答by Jay

Add following Maven dependency

添加以下 Maven 依赖项

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m08</version>
</dependency>  

回答by MPeli

I had a very similar problem. When I fixed the exception by adding dependency (javax.ws.rs), I was then unable to use javax.ws.rs.client.ClientBuilder. I made the following change and the problem seems to be gone.

我有一个非常相似的问题。当我通过添加依赖项 (javax.ws.rs) 修复异常时,我无法使用 javax.ws.rs.client.ClientBuilder。我做了以下更改,问题似乎消失了。

file web.xml

文件 web.xml

<servlet>
 <servlet-name>Jersey Web Application</servlet-name>
 - <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 + <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
 - <param-name>jersey.config.server.provider.packages</param-name>
 + <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.test.test1</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
<servlet>