JAX-RS:Tomcat正常运行但未找到root(404)

时间:2015-10-05 05:05:26

标签: java web-services rest tomcat servlets

我刚刚为我的RESTful Web API启动了JAX-RS(Jersey)。 Tomcat正常运行但未找到root(错误404)。

错误控制台如下:

SEVERE: Servlet [Jersey REST Service] in web application [/com.G8.ws] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1858)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1709)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5253)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5543)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

我的简单代码如下,因为我的包名是com.G8.ws,我发了http://localhost:8080/com.G8.ws

package com.G8.ws;


import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/v1/status")
public class V1_status {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String returnTitle(){
        return "<p>RESTful Web API using JAX/RS</p>";
    }

}

以下是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.G8.ws</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
  <!--   <param-name>com.sun.jersey.config.property.packages</param-name>-->
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.G8.ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

</web-app>

而且,我已经在jersey-archive-1.19下载了所有jar文件。

可能有什么不对?

1 个答案:

答案 0 :(得分:1)

您需要2.x捆绑或将声明的ServletContainer更改为1.x version

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

如果您更改为1.x版本,那么您将使用注释掉的包扫描属性

<param-name>com.sun.jersey.config.property.packages</param-name>