【问题标题】:Error 404 on Tomcat Rest exampleTomcat Rest 示例上的错误 404
【发布时间】: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


    【解决方案1】:

    你需要注册与你的路径相关的End Point,更改RestApplication:

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

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

    【讨论】:

    • 感谢您的回复,可惜没有解决404错误
    • 你编译部署到tomcat没有任何错误吗?
    • 我得到了这个原因:org.apache.catalina.LifecycleException: A child container failed during start
    • 你可以为问题添加完整的例外吗?
    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2014-10-27
    • 2010-11-12
    • 2017-02-13
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多