Tomcat Rest示例中的错误404

时间:2017-09-18 09:30:34

标签: java rest tomcat glassfish jax-rs

我试图用Tomcat,JAX-RS和Jersey创建一个简单的Rest API项目。 我遇到的问题是,当我执行get时,我从Web浏览器收到错误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我所在的人(应用程序的模型)和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

我哪里错了?

1 个答案:

答案 0 :(得分:1)

您需要注册与您的路径相关的终点,更改RestApplication:

public class RestApplication extends ResourceConfig {
public RestApplication () {
    register(PersonEndpoint.class);
}

Person不是路径的一部分,您应该致电http://localhost:8080/rest/persons