我正在尝试使用eclipse
中的maven
和Apache Tomcat
制作RESTFUL JAVA WEB服务。对我来说似乎一切都很好,并没有显示任何错误,但是当打开它显示404 Error
的URL时。
我试图通过此网址http://localhost:8080/first/rest
调用它
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>first</display-name>
<servlet>
<servlet-name>firstapp</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.first.pkg</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>firstapp</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
的的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>task</groupId>
<artifactId>first</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>first Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>
的 RestServ.java
package com.first.pkg;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/")
public class RestServ {
@GET
@Produces("text/html")
public Response getStartingPage()
{
String output = "<h1>Hello World!<h1>" +
"<p>RESTful Service is running ... <br>Ping @ " + new Date().toString() + "</p<br>";
return Response.status(200).entity(output).build();
}
}
答案 0 :(得分:2)
<display-name>first</display-name>
是一个描述部分,我认为您不能通过此名称调用您的Web服务。相反,尝试将其称为
http://localhost:8080/{nameOfYourWARFileHere}/rest
还要确保在斜杠之后立即将rest
放入RestServ.java中的@Path("/")
注释中,否则请求映射将不起作用。