gpt4 book ai didi

java - Tomcat Rest 示例上的错误 404

转载 作者:行者123 更新时间:2023-11-28 23:57:15 24 4
gpt4 key购买 nike

我正在尝试使用 Tomcat、JAX-RS 和 Jersey 创建一个简单的 Rest API 项目。我面临的问题是,当我执行 get 时,我从 Web 浏览器收到错误 404。这些是我的文件:

网络.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

我哪里错了?

最佳答案

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

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

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

关于java - Tomcat Rest 示例上的错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com