Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Can a JavaFX Client Invoke a Java Web Service?

DZone's Guide to

Can a JavaFX Client Invoke a Java Web Service?

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

This tip of the day is inspired by Learning JavaFX Script - Part 3. The original article explains how to invoke a Web service from a JavaFX client using NetBeans 5.5.1 and GlassFish v1. Newer version of both NetBeans and GlassFish are available since the article was written. This tip of the day explains how to invoke a Metro endpoint deployed on GlassFish v2 from a JavaFX client - all using NetBeans 6.

  1. Following screencast #ws7, create a plain (without Security or Reliability enabled) Metro endpoint using NetBeans 6 and GlassFish v2.
  2. In NetBeans 6 IDE, install the JavaFX plugin as described here.
  3. Create Web service client library - Creating a Web service client in JavaFX Script Application is causing a NullPointerException (issue #126352). The workaround I used is to create a separate library with client-side artifacts and then include it as dependency in the JavaFX client project.
    • Create a new project of type "Java Class Library" as shown below:



      and click on "Finish".
    • Enter the project name as "MetroClientLibrary" as shown below:



      and click on "Finish".
    • Right-click on the newly created project, select "New", "Web Service Client...".
    • Click on "Browse..." button next to "Project" radio button and select the deployed Web service from Metro endpoint project. If the Web service is deployed on a different machine then you may specify the WSDL URL. Specify the package name "client" as shown below:



      and click on "Finish".
    • Once the Web service client-side artifacts are generated (indicated by expandable Web Service References tree node), right-click on the project and select "Build". This generates a JAR file that will be utilized later. The location of this jar file is shown in the Output console. In our case, it is

      C:\workarea\samples\javafx\MetroClientLibrary\dist\MetroClientLibrary.jar.
  4.  

  5. Create a JavaFX Project
    • Create a new JavaFX project by right-clicking in the Project explorer, selecting "New Project" and entering the values as shown below:

    • Click on "Next >" and enter the values as shown below:



      and click on "Finish".
    • Right-click on the newly created project, "Properties", "Libraries", "Add JAR/Folder" and select the JAR file created in "MetroClientLibrary" project as shown below:



      and click on "OK".

      Notice, Java SE 6 U4 is used to compile and run this project. If you are using an earlier version of Java SE 6, then you need to override JAX-WS 2.1 and JAXB 2.1 jars using endorsed mechanism as explained here. The classes in these jars are already bundled in Java SE 6 U4.
    • In metroclient.Main.fx file, replace "// place your code here" with the following code:

      import java.lang.*;
      import javafx.ui.*;

      import client.NewWebServiceService;
      import client.NewWebService;

      class InputModel {
      attribute name: String?;
      }
      var inputModel = InputModel { };
      var nameField = TextField { };
      nameField.action = operation() {
      inputModel.name = nameField.value;
      };

      class ButtonClickModel {
      attribute result: String;
      }
      var model = new ButtonClickModel();

      Frame {
      title: "JavaFX Client -> Metro endpoint"
      width: 350
      height: 200
      content: GridPanel {
      rows: 3
      vgap: 5
      cells:
      [SimpleLabel {
      text: "Name : "
      },
      nameField,
      SimpleLabel {
      text: "Result from endpoint : "
      },
      Label {
      text: bind "{model.result}"
      },
      Button {
      text: "Invoke Web Service!"
      action: operation() {
      do {
      try {
      var service: NewWebServiceService = new NewWebServiceService();
      var port: NewWebService = service.getNewWebServicePort();
      var name: String = "{nameField.value}";
      var result: String = port.sayHello(name);
      System.out.println("response: {result}");
      model.result = result;
      } catch (e:Exception) {
      System.out.println("exception: {e}");
      }
      }
      }
      }
      ]
      }
      visible: true
      };
  6. Invoke the JavaFX client project
    • Right-click on the recently create project ("MetroClient") and select "Run Project". The following window is displayed:

    • Enter "Duke" in the text box and click on "Invoke Web Service!" button to see the result as shown below:

    After following these steps, you have created a JavaFX client that can invoke a Metro endpoint project deployed on GlassFish - all using NetBeans IDE.

    Now Metro provides secure, reliable, transactional and .NET 3.0 interoperable Web service. Have you tried/used any of those features in Metro ?

    Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.

    Technorati: totdd javafx metro glassfish netbeans webservices

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Arun Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}