泽西servlet异常tomcat jersey

时间:2015-11-03 02:13:36

标签: maven tomcat java-ee servlets jersey

我在尝试使用maven将servlet部署到Tomcat时遇到问题; 我在其他帖子中一直在阅读这个问题,但仍无法通过答案/建议取得成功。

无论我在尝试什么,我都会得到这个例外:

  

javax.servlet.ServletException:servlet的Servlet.init()Jersey Web应用程序抛出异常       org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)       org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)       org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615)       org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       java.lang.Thread.run(Thread.java:745)   根本原因   com.sun.jersey.api.container.ContainerException:ResourceConfig实例不包含任何根资源类。       com.sun.jersey.server.impl.application.RootResourceUriRules。(RootResourceUriRules.java:99)       com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359)       com.sun.jersey.server.impl.application.WebApplicationImpl.access $ 700(WebApplicationImpl.java:180)       com.sun.jersey.server.impl.application.WebApplicationImpl $ 13.f(WebApplicationImpl.java:799)       com.sun.jersey.server.impl.application.WebApplicationImpl $ 13.f(WebApplicationImpl.java:795)       com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)       com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)       com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)       com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509)       com.sun.jersey.spi.container.servlet.ServletContainer $ InternalWebComponent.initiate(ServletContainer.java:339)       com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)       com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)       com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394)       com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577)       javax.servlet.GenericServlet.init(GenericServlet.java:158)       org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)       org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)       org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)       org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)       org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)       org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)       java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)       java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615)       org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       java.lang.Thread.run(Thread.java:745)

这是我的web.xml文件:

<servlet>
    <servlet-name>Jersey Web Application</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.rest.example
    </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

我的pom.xml

<dependencies>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>asm.jar</artifactId>
    <version>20041228.180559</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>jersey-bundle.jar</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>jersey-server.jar</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>jersey-core.jar</artifactId>
    <version>1.8</version>
    <type>pom.lastUpdated</type>
</dependency>

sum.java文件

package com.rest.example;

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

public class sum {
@Path("/hello")

@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() 
{
        return "Hello World";
    }

}

以防万一:

and the project itself

我已经尝试过清洁并再次建造但仍然没有变化; 我错过了什么?感谢。

2 个答案:

答案 0 :(得分:10)

错误讯息:

  

ResourceConfig实例不包含任何根资源类。

<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.rest.example</param-value>
</init-param>

以上内容告诉Jersey在com.rest.example包中扫描注释了@Provider@Path的类,以便它可以注册它们。该错误告诉您在应用程序加载期间没有注册资源类。原因是:

public class sum {
@Path("/hello")

@Path注释应该在类定义的 top 上,而不是下面。 Jersey发现没有资源类,因为类上没有@Path注释。

答案 1 :(得分:0)

我遇到了同样的问题,即使有这个设置也进入了一个问题 javax.servlet.ServletException:servlet [jersey-servlet]的Servlet.init()抛出异常 引起的 java.lang.ArrayIndexOutOfBoundsException:14877

问题是最终我对包不够具体,我的项目在主包目录中变得很大。 请注意并收集项目中分隔的包中的所有端点。