2014-05-21 185 views
1

我有一個玩具REST Web服務,我試圖部署到Tomcat 7.0服務器。問題是我甚至無法啓動服務器。這裏是我得到的錯誤消息:Tomcat 7.0服務無法啓動ClassNotFoundException:javax.ws.rs.ProcessingException

SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorldRestService]] at java.util.concurrent.FutureTask.report(FutureTask.java:133) at java.util.concurrent.FutureTask.get(FutureTask.java:199) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:273) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1170) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:640) at java.lang.Thread.run(Thread.java:853) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorldRestService]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) ... 6 more

Caused by: java.lang.NoClassDefFoundError: javax.ws.rs.ProcessingException at java.lang.J9VMInternals.prepareClassImpl(Native Method) at java.lang.J9VMInternals.prepare(J9VMInternals.java:1122) at java.lang.Class.getDeclaredFields(Class.java:685) at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106) at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:263) at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:142) at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:67) at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:405) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:881) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 6 more Caused by: java.lang.ClassNotFoundException: javax.ws.rs.ProcessingException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) ... 20 more

SEVERE: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]] at java.util.concurrent.FutureTask.report(FutureTask.java:133) at java.util.concurrent.FutureTask.get(FutureTask.java:199) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.startup.Catalina.start(Catalina.java:691) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:94) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) at java.lang.reflect.Method.invoke(Method.java:619) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(FutureTask.java:273) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1170) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:640) at java.lang.Thread.run(Thread.java:853) Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:800) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 6 more

SEVERE: The required Server component failed to start so Tomcat is unable to start. org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.startup.Catalina.start(Catalina.java:691) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:94) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) at java.lang.reflect.Method.invoke(Method.java:619) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 7 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 9 more Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 11 more

我使用的是Jersey 2.x.我這裏還有我的罐子:

javax.ws.rs-API-2.0-m10.jar

球衣-client.jar中

球衣-common.jar

球衣,容器的servlet的.jar

Jersey的容器servlet的core.jar添加

球衣-的server.jar

我的web.xml看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<display-name>HelloWorldRestService</display-name> 
<servlet> 
    <servlet-name>java.helloworldrestservice.resources.HelloWorldApplication</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 

    <!-- Register JAX-RS Application, if needed. --> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>java.helloworldrestservice.resources.HelloWorldApplication</param-value> 
    </init-param> 

    <!-- Register resources and providers under my.package. --> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>java.helloworldrestservice.resources</param-value> 
    </init-param> 

    <!-- Enable Tracing support. --> 
    <init-param> 
     <param-name>jersey.config.server.tracing</param-name> 
     <param-value>ALL</param-value> 
    </init-param> 

    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>java.helloworldrestservice.resources.HelloWorldApplication</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

HelloWorldApplication.java:

package java.helloworldrestservice.resources; 

import java.util.HashSet; 
import java.util.Set; 
import javax.ws.rs.core.Application; 


public class HelloWorldApplication extends Application 
{ 

@Override 
public Set<Class<?>> getClasses() 
{ 
    //log.info("starting the HelloWorld application..."); 
    Set<Class<?>> set = new HashSet<Class<?>>(); 
    set.add(HelloWorld.class); 
    return set; 
} 
} 

HelloWorld.java:

package java.helloworldrestservice.resources; 


import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Path("/hello") 
public class HelloWorld { 
@GET 
@Produces(MediaType.TEXT_PLAIN) 
public String sayPlainTextHello() 
{ 
    return "Hello World!!"; 
} 
} 

有什麼不對?爲什麼我會看到這個缺失的ProcessingException類,我該如何擺脫這個錯誤?

非常感謝!

回答

0

javax.ws.rs.ProcessingException似乎是javax.ws.rs包的一部分,該包是rest的一部分,但默認情況下未隨Java或Tomcat提供。

它似乎包含在其中,包括javax.ws.rs-api-2.0.jar。 Maven倉庫中還有一個可用的javax.ws.rs版本。

它似乎是澤西參考,但不執行此類。

https://jersey.java.net/apidocs/2.6/jersey/javax/ws/rs/class-use/ProcessingException.html

所以你將需要下載相應的jar並將其包含在類路徑,以便它能夠使用ProcessingException並開始你的容器。

做了一些看。我希望希望 Jersey.java.net將提供這個庫作爲他們的捆綁包的一部分。如果他們不這樣做,那麼您將不得不自己將Maven的REST先決條件從中拉出來。

0

對不起,文本不是專門針對你的問題(但它應該完成工作)。我回答了類似的問題,所以我只是重複使用它。

如果您使用的是標準的Tomcat安裝(或其他一些servlet容器),AFAIK不能避免明確告訴它在web.xml *文件中啓動哪些servlet。由於無論如何您必須使用web.xml,要使平穩的Web服務正常工作,最簡單的方法是忘記完全擴展javax.ws.rs.core.Application,並僅指定那裏的上下文路徑。您仍然可以使用標準的jax-rs註釋來聲明實際的Web服務。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0" 
> 
    <servlet> 
    <servlet-name>rest-test</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>com.domain.mypackage</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name> rest-test</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

兩個值得注意的地方:

  1. 您將需要捆綁REST實現在你的WAR文件,因爲servlet容器通常不包含一個。由於Jersey是JAX-RS的參考實現,這就是我在上面的servlet-class元素中使用的那個。如果需要,您可以用Apache CXF實現替換它。

  2. init-param元素告訴澤西你的哪些包用於搜索帶有Web服務註釋的Java文件。編輯這個指向您的Web服務。請注意,如果您選擇使用Apache CXF而不是Jersey,則任何init-param元素中所需的內容將有所不同。有人知道CXF請發表他們的想法。

如果您使用的是Maven,只需添加一個依賴於jersey-servletpom.xml文件的dependencies部分:

<dependencies> 
    <dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-servlet</artifactId> 
    <version>1.18.2</version> 
    </dependency> 
    ... 
</dependencies> 

在此之後,宣告您的Web服務是直截了當使用標準的JAX -RS註釋在你的Java類中:

package com.domain.mypackage; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.Produces; 
import javax.ws.rs.GET; 
import javax.ws.rs.MatrixParam; 
import javax.ws.rs.Path; 

// It's good practice to include a version number in the path so you can have 
// multiple versions deployed at once. That way consumers don't need to upgrade 
// right away if things are working for them. 
@Path("calc/1.0") 
public class CalculatorV1_0 { 
    @GET 
    @Consumes("text/plain") 
    @Produces("text/plain") 
    @Path("addTwoNumbers") 
    public String add(@MatrixParam("firstNumber") int n1, @MatrixParam("secondNumber") int n2) { 
    return String.valueOf(n1 + n2); 
    } 
} 

這應該就是你所需要的。如果你的Tomcat安裝正在端口8080上本地運行,並部署WAR文件上下文myContext,要

http://localhost:8080/myContext/rest/calc/1.0/addTwoNumbers;firstNumber=2;secondNumber=3 

...應該產生預期的結果(5)。

乾杯!

*有人請糾正我,如果你知道如何在不使用web.xml的情況下將Jersey servlet添加到Tomcat中的上下文 - 可能是使用上下文或生命週期偵聽器?