如何从Enterprise Application Client使用restful Web服务?

时间:2017-03-22 18:05:40

标签: java swing web-services rest jms

我使用Swing应用程序的自动生成功能在正常的Swing应用程序上使用休息Web服务,但现在我尝试为restful客户端复制相同的代码并尝试在Netbeans的企业应用程序客户端上使用它,我得到一些错误信息。

这是我收到的错误消息。

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/ws/rs/client/ClientBuilder
    at currencyExchange.currencyExchangerClient.<init>(currencyExchangerClient.java:34)
    at com.sushan.displays.ExchangeWindow.jButton1ActionPerformed(ExchangeWindow.java:125)
    at com.sushan.displays.ExchangeWindow.access$100(ExchangeWindow.java:18)
    at com.sushan.displays.ExchangeWindow$2.actionPerformed(ExchangeWindow.java:62)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.client.ClientBuilder
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at org.glassfish.appclient.client.acc.ACCClassLoader.findClass(ACCClassLoader.java:237)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 40 more

在企业应用程序客户端中使用休眠客户端有不同的方法吗?

这是我用来使用restful web服务的代码:

private WebTarget webTarget;
private Client client;
private static final String BASE_URI = "http://localhost:8080/RealEstateWebsite-war/webresources";

public currencyExchangerClient() {
    client = ClientBuilder.newClient();
    webTarget = client.target(BASE_URI).path("RESTCurrencyExchangingService");
}

public String getXml(String currencyType, String amount) throws ClientErrorException {
    WebTarget resource = webTarget;
    resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{currencyType, amount}));
    return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(String.class);
}

public void putXml(Object requestEntity) throws ClientErrorException {
    webTarget.request(javax.ws.rs.core.MediaType.APPLICATION_XML).put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_XML));
}

public void close() {
    client.close();
}

我尝试使用企业应用程序客户端的原因是因为我找不到在正常的swing应用程序中实现消息监听器的方法。我需要这个桌面应用程序能够使用restful web服务以及监听消息并使用它们来显示我在消息驱动bean中注册的主题。 我知道如何仅将JMS用于企业应用程序客户端,这使我感到困惑,并且我没有得到任何预编译错误,如果我错过了库或其他什么的话。初始化其余客户端时,我只会收到此错误。

2 个答案:

答案 0 :(得分:0)

根据此错误,swing应用程序仍然缺少正在企业应用程序中使用的库。也许代码正在编译,因为缺少的库只在运行时才需要。

答案 1 :(得分:0)

将所需的jar文件放在类路径中。还要在部署程序集中放置REST所需的所有jar文件,然后清理构建项目,然后进行部署。