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

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

When I hit one of the endpoints of the war, the code seems to work and returns the answer without any problems from the point of view of the server, but the client receives a 500 response returned from tomcat. This is mistake:

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 does not appear in the tomcat log.

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

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

But this dependency does not seem to have this class. However, this class is in this dependency:

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

It seems uncomfortable to me to downgrade to a lower version, although this is a milestone. More importantly, jar does not include classes that I use in my code, such as javax.ws.rs.client.ClientBuilder .

Can someone explain why I am getting this exception and how to solve this problem?


My workaround

I came to the conclusion that this is more due to the fact that the Jersey client and CXF interfere 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

 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider; 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 JacksonJaxbJsonProvider() ); 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()); } } 

A very similar API. I just searched and replaced the method names and it worked.

+5
source share
3 answers

I also ran into this problem. Unlike you, the project requirements meant that I needed to play Jersey and CXF.

The final problem is the FactoryFinder class, which Jersey uses to get the appropriate execution delegate (used for various helper objects) - this, in turn, has a class with import into the intruder class.

To get around this, you need to have a file in your resource folder (suppose it is imported into your classes folder) using the META-INF / services / javax.ws.rs.ext.RuntimeDelegate path, which contains the value:

 org.glassfish.jersey.internal.RuntimeDelegateImpl 

This should solve your problem.

+3
source

Add the following Maven dependency

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

I had a very similar problem. When I fixed the exception by adding the 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.

file 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> 
-1
source

Source: https://habr.com/ru/post/1210765/


All Articles