我正在编写一个有一些事件的Java应用程序,我需要一个能够添加新事件和取消现有事件的客户端。我编写了一种方法,该方法应该连接到Web服务并通过它取消事件。但是我在与服务的连接上遇到了麻烦,并且不断收到这个Exception
:
java.lang.RuntimeException: Failed : HTTP error code : 404
at org.unibl.etf.mdp.event.gui.EventAddCancelClient.cancelEvent(EventAddCancelClient.java:131)
at org.unibl.etf.mdp.event.gui.EventController.cancelEvent(EventController.java:39)
at org.unibl.etf.mdp.event.gui.EventForm.buttonCancelActionPerformed(EventForm.java:86)
at org.unibl.etf.mdp.event.gui.EventForm.access$0(EventForm.java:85)
at org.unibl.etf.mdp.event.gui.EventForm$2.actionPerformed(EventForm.java:64)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:269)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6578)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3343)
at java.desktop/java.awt.Component.processEvent(Component.java:6343)
at java.desktop/java.awt.Container.processEvent(Container.java:2259)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4961)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2317)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4539)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4480)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2303)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2758)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:766)
at java.desktop/java.awt.EventQueue.access$500(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:717)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:711)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:99)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:739)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:737)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:736)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:199)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
这是我用来连接服务的方法的代码:
public static void cancelEvent(int id) {
try {
URL url = new URL(BASE_URL + id); //BASE_URL = "http://localhost:8080/InfoEvent/service/events/"
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
OutputStream os = conn.getOutputStream();
// os.write("");
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
os.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
我也不知道该方法中的这部分代码该怎么做:
os.write("");
这是服务中用来取消事件的方法:
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/cancel/{id}")
public static Response setCanceled(@PathParam("id") int id) {
if(EventSetup.cancelEvent(id)) return Response.status(200).entity(EventSetup.getById(id)).build();
else return Response.status(500).entity("Error while canceling event!").build();
}
答案 0 :(得分:2)
您收到404,这意味着该URL不存在。
尝试使用有效的ID将POSTMAN命中此URL
http://localhost:8080/InfoEvent/service/events/ID
您会看到您将得到一个404。因此,基本上您的端点不正确,或者应该接收该调用的服务未在您的计算机上运行并启动(因为它是localhost)。