一尘不染

JAX-RS:Tomcat正常运行,但找不到根目录(404)

tomcat

我刚刚为我的RESTful Web API启动了JAX-RS(泽西岛)。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>

而且,我已将所有jar文件下载到jersey-archive-1.19

有什么事吗


阅读 235

收藏
2020-06-16

共1个答案

一尘不染

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

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

如果更改为1.x版本,则应改用注释掉的程序包扫描属性

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