【发布时间】:2017-09-18 09:30:33
【问题描述】:
我正在尝试使用 Tomcat、JAX-RS 和 Jersey 创建一个简单的 Rest API 项目。 我面临的问题是,当我执行获取时,我从网络浏览器收到错误 404。这些是我的文件:
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>User Management</display-name>
<servlet>
<servlet-name>Jersey RESTful Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.rest</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey RESTful Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
在 src/main/java 中,我需要打包:com.example,其中我有 Person(应用程序的模型)和 RestApplication,而在 com.rest 中我有 PersonEndpoint.java:
@Path("/persons")
@Produces("application/json")
@Consumes("application/json")
public class PersonEndpoint {
@POST
public Response create(final Person person) {
// TODO: process the given person
// you may want to use the following return statement, assuming that
// Person#getId() or a similar method
// would provide the identifier to retrieve the created Person resource:
// return
// Response.created(UriBuilder.fromResource(PersonEndpoint.class).path(String.valueOf(person.getId())).build()).build();
return Response.created(null).build();
}
@GET
@Path("/{id:[0-9][0-9]*}")
public Response findById(@PathParam("id") final Long id) {
// TODO: retrieve the person
Person person = null;
if (person == null) {
return Response.status(Status.NOT_FOUND).build();
}
return Response.ok(person).build();
}
@GET
public List<Person> listAll() {
// TODO: retrieve the persons
final List<Person> persons = new ArrayList<>();
persons.add(new Person("Mario", "Rossi", 123));
return persons;
}
@PUT
@Path("/{id:[0-9][0-9]*}")
public Response update(@PathParam("id") Long id, final Person person) {
// TODO: process the given person
return Response.noContent().build();
}
@DELETE
@Path("/{id:[0-9][0-9]*}")
public Response deleteById(@PathParam("id") final Long id) {
// TODO: process the person matching by the given id
return Response.noContent().build();
}
}
RestApplication.java
@ApplicationPath("/rest")
public class RestApplication extends Application {
}
我尝试使用的网址是:http://localhost:8080/People/rest/persons
我哪里错了?
【问题讨论】:
标签: java rest tomcat glassfish jax-rs