繁体   English   中英

通过 RESTful Web 服务编辑对象

[英]Editing an object through a RESTful web service

我正在编写一个 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();
}

你得到 404 这意味着 URL 不存在。

尝试使用有效 ID 通过 POSTMAN 访问此 URL

http://localhost:8080/InfoEvent/service/events/ID

你会看到你会得到一个 404。所以基本上你的端点不正确或者应该接收该调用的服务没有在你的计算机中运行和启动(因为它是本地主机)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM