我正在尝试使用apache Tomcat部署一个简单的Jax- RS应用程序。服务器在我的本地主机上正确运行,tomcat主页打开。但是,当我在服务器上从eclipse运行时,出现状态404错误。有人知道这是为什么吗?
Pom.xml
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javaee8</groupId>
<artifactId>hello-javaee8</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hello-javaee8 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>hello-javaee8</finalName>
</build>
</project>
Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
如果需要添加其他文件以找到此解决方案,请告诉我
有两个原因。
首先,您不包括JAX-RS实现,仅包括JAX-RS API。 JAX-RS API仅提供一组接口,并留待实现来提供这些接口的具体实现。
如果您没有JAX-RS实现,我建议您选择Jersey(参考实现)。将以下内容添加到您的pom.xml
:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.29</version>
</dependency>
第二,您尚未在web.xml
的任何位置注册您的应用程序。如何执行此操作取决于所使用的JAX-RS实现以及注册类和提供程序的方式。为了帮助您解决这个问题,我需要您回答几个问题:
Application
的类(或扩展了ResourceConfig
的类?]]@Path
注释,并且所有提供者都用@Provider
注释?如果使用泽西岛并且对第一个问题回答是,请在web.xml
中放置以下内容:
<servlet> <servlet-name>Jersey Web Application</filter-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value><!-- FQN of your Application/ResourceConfig class --></param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
否则,请查看您特定的JAX-RS框架的设置说明。