![](/img/trans.png)
[英]HTTP Status 404 - Not Found in pages when deploying war of Spring application in dockerized payara
[英]HTTP Status 404 Not Found For Jersey REST application with Eclipse and Payara server
我正在尝试使用Payara服务器在Eclipse中为预订系统开发Jersey服务器应用程序。 运行项目时,出现“ HTTP状态404-找不到”。 我检查了一些教程和StackOverflow帖子,但是找不到错误。 有人可以帮我吗?
BookingService接口:
public interface BookingService {
public Response addBooking(Room room);
public Response cancelBooking(Room room);
public Room getRoom(int roomNumber);
public Room[] getAllAvailableRooms();
}
BookingService的实现:
@Path("/booking")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookingServiceImpl implements BookingService{
private static Map<Integer,Room> rooms = new HashMap<Integer,Room>();
@POST
@Path("/add")
public Response addBooking(Room room) {
Response response = new Response();
if(rooms.get(room.getNumber()).isBooked()) {
response.setMessage("This room is not available");
return response;
}
room.bookRoom();
response.setMessage("Room successfully booked");
return response;
}
@GET
@Path("/delete")
public Response cancelBooking(Room room) {
Response response = new Response();
if(!rooms.get(room.getNumber()).isBooked()) {
response.setMessage("This room is not booked so booking cannot be cancelled");
return response;
}
room.cancelBooking();
response.setMessage("The booking for room " + room.getNumber() + "has been successfully cancelled");
return null;
}
@GET
@Path("/getAll")
public Room[] getAllAvailableRooms() {
Set<Integer> keys = rooms.keySet();
Room[] roomArray = new Room[keys.size()];
int i=0;
for(Integer key : keys){
if (!rooms.get(key).isBooked()) {
roomArray[i] = rooms.get(key);
i++;
}
}
return roomArray;
}
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>se.kth.id1212</groupId>
<artifactId>ID1212BookingSystem</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ID1212BookingSystem Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.20</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ID1212BookingSystem</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
编辑:研究问题时,我读到使用jersey 2时,web.xml文件除了显示名称外无需指定任何其他内容,我不知道这是不是真的,但是下面是我之前使用的web.xml文件阅读,但它也不起作用。
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- Jersey Servlet configurations -->
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>se.kth.id1212.ID1212BookingSystem</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Jersey Servlet configurations -->
</web-app>
编辑2,添加了以下图像:
即使最新的web.xml出现问题,您也需要放慢脚步,并将每个更改与本教程进行比较。 根据提到的教程
您应该在web.xml中的init-param下面
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>se.kth.id1212.ID1212BookingSystem</param-value>
</init-param>
但是,相反,您写了
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>se.kth.id1212.ID1212BookingSystem</param-value>
</init-param>
另外,仅作说明, /add
期望POST
,这意味着它将期望其主体中的请求数据。 我建议使用一些REST客户端,可能是POSTMAN进行测试。
==更新==
我真的不明白,您为什么要在web.xml
使用以下包名称?
<param-value>se.kth.id1212.ID1212BookingSystem</param-value>
我是明确的,什么地方,你有没有失误的包BookingServiceImpl
应server.service
不se.kth.id1212.ID1212BookingSystem
。 因此,在param-value
, 应该提供相同的程序包名称,而不是随机的。 。 所以应该
<param-value>server.service</param-value>
另外,我知道,您在类上使用了Produces
/ Consumes
批注,而应该在每个方法上使用
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookingServiceImpl implements BookingService{
这是我的要求,不要将您的知识与可用的教程混在一起。 首先, 完全按照任何可用的教程(可能只是复制/粘贴)来尝试理解实现。 选择最简单的教程可能只是打印Hello World
您的应用程序似乎未定义任何侦听根URL的REST端点。 如果您的应用程序在诸如http:// localhost / myapp之类的URL上运行,则获取该URL的HTTP 404响应是完全正常的。 您只能从绑定到某些REST方法的URL中获得有效的响应。
由于BookingServiceImpl
绑定到“ / booking”,并且其所有方法都绑定到“ / add”或“ / getAll”之类的子路径,因此您可以通过组合使用来访问REST方法
BookingServiceImpl
路径,即“ / booking” BookingServiceImpl
方法,例如“/ GETALL”为getAllAvailableRooms
因此,要调用getAllAvailableRooms
方法,您应该使用诸如http:// localhost / myapp / booking / getAll之类的URL,而不仅要使用http:// localhost / myapp 。
在最新的Payara Server版本中,当您转到应用程序详细信息并单击底部的“模块和组件”表中的“查看端点”时,应该能够在Web管理控制台中查看所有可用的URL,请参阅结束。
补充笔记:
我建议在单独的类中定义JAX-RS应用程序,该类扩展javax.ws.rs.Application
并接受带有servlet映射的@ApplicationPath批注。 然后,您的web.xml中不需要任何内容,甚至可以将其删除。 这是与您的web.xml等效的Java代码(是的,就是这么简单,所有其他代码都是可选的):
@ApplicationPath("/") // maps the servlet to "/*"
public class MyRESTApplication extends Application {
}
我还建议观看一段简短的视频,介绍如何在Netbeans中使用Payara创建REST服务(在Eclipse中应该非常相似,IDE无关紧要)。 您可以跳过简介,从3:30开始。
Payara管理控制台中的REST端点详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.