【发布时间】:2015-01-26 09:13:24
【问题描述】:
我遵循了许多示例和教程,试图让我的 REST Web 服务运行,但没有任何成功。我正在使用 IntelliJ 13、Maven3、Java 7 和 Tomcat 7。
我的 pom.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jafwatt</groupId>
<artifactId>rest-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
我的 web.xml 看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>rest</display-name>
<servlet>
<servlet-name>restServlet</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.jafwatt.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
CustomerResource.java
package com.jafwatt.rest;
import com.jafwatt.rest.entity.Customer;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@Path("/customers")
public class CustomerResource {
private Map<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>();
private AtomicInteger idCounter = new AtomicInteger();
@GET
@Path("/hello")
@Produces("text/plain")
public String getCustomers() {
return "Hello";
}
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is) {
Customer customer = readCustomer(is);
customer.setId(idCounter.incrementAndGet());
customerDB.put(customer.getId(), customer);
System.out.println("Created customer " + customer.getId());
return Response.created(URI.create("/customers/"
+ customer.getId())).build();
}
@GET
@Path("{id}")
@Produces("application/xml")
public StreamingOutput getCustomer(@PathParam("id") int id) {
final Customer customer = customerDB.get(id);
if (customer == null) {
throw new WebApplicationException(
Response.Status.NOT_FOUND);
}
return new StreamingOutput() {
public void write(OutputStream outputStream)
throws IOException, WebApplicationException {
outputCustomer(outputStream, customer);
}
private void outputCustomer(OutputStream outputStream, Customer customer) {
}
};
}
@PUT
@Path("{id}")
@Consumes("application/xml")
public void updateCustomer(@PathParam("id") int id,
InputStream is) {
Customer update = readCustomer(is);
Customer current = customerDB.get(id);
if (current == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
current.setFirstName(update.getFirstName());
current.setLastName(update.getLastName());
current.setStreet(update.getStreet());
current.setState(update.getState());
current.setZip(update.getZip());
current.setCountry(update.getCountry());
}
private Customer readCustomer(InputStream is) {
return new Customer();
}
}
Maven 构建 WAR 时没有错误,而 Tomcat 部署 WAR 时没有错误,但是 URL 的组合除了 404 响应之外没有其他任何内容。
根上下文是“rest”和我尝试过的 URL:
http://localhost:8090/rest
http://localhost:8090/rest/custsomers
http://localhost:8090/rest/custsomers/hello
我知道 Tomcat 正在该端口上运行,因为我可以访问管理器应用程序。
我已经阅读了很多关于 Stackoverflow 和其他地方的帖子,但我真的很沮丧,应该是一个相对简单的任务却超出了我的能力范围。
请帮忙。
【问题讨论】:
-
调用 API 时会发生什么?是 404 吗?
-
请在您的问题中添加确切的错误消息;还包括一个示例请求 URL,以便我们了解您如何调用您的服务。
-
我在开车回家的路上意识到我错过了那个小细节。编辑解释我得到 404。
-
请发布您使用的确切网址。我猜这是你打错端口号的事实。但这只是一个猜测。
-
@user1493954 我已经用 URL 编辑了我的 OP,但这不是端口号问题。我可以访问 Tomcat 服务器,但无法访问我的 Web 服务。
标签: java web-services rest tomcat jax-rs