Getting started with JAX-WS

JAX-WS stands for Java API for XML Web Services. It is a Java programming language API for creating web services and clients that communicate using XML. This post is a quick start for JAX-WS.

Prerequisites

GlassFish integrated with Eclipse.

Creating the JAX-WS Web Service

1.In Eclipse create a Dynamic Web Project called ‘com.eviac.blog.jaxwsproj’. Make GlassFish as the Target Runtime.

          

2.Create a new class called ‘SampleWS’ in the created project. This will be the implementation class of the web service.

SampleWS.java

package com.eviac.blog.jaxws.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class SampleWS {

 @WebMethod
 public int sum(int a, int b) {
  return a + b;
 }

 @WebMethod
 public int multiply(int a, int b) {
  return a * b;
 }

}

3.Open a terminal and navigate to the root of the project directory. Create a directory called wsdl inside WebContent/WEB-INF/. Use the following command to create web service artifacts. Make sure your JAVA_ HOME is set properly or this command will not work. Also make sure to build the project before running this command or it will complain class not found.

wsgen -classpath build/classes/ -wsdl -r WebContent/WEB-INF/wsdl -s src -d build/classes/ com.eviac.blog.jaxws.service.SampleWS

4.Refresh the project to discover created artifacts. Open the created WSDL-file inside wsdl folder. Search for REPLACE_WITH_ACTUAL_URL and replace it with the web service URL: http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService, and save the file.

5.Deploy the project in Glassfish by right-clicking the project, click Run As -> Run on Server and select the Glassfish server.



Creating the JAX-WS client

1.Create a Java project in eclipse called ‘com.eviac.blog.jaxwsclientproj’. Open up a new terminal and go to the project root. Use the following command to generate the classes you need to access the web service. Here you will need to use the URL of the WSDL file.

wsimport -s src -d bin http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl

2.Create a new class called ‘SampleWSClient’ in the project.


SampleWSClient.java

package com.eviac.blog.jaxws.client;

import javax.xml.ws.WebServiceRef;

import com.eviac.blog.jaxws.service.SampleWS;
import com.eviac.blog.jaxws.service.SampleWSService;

public class SampleWSClient {

 @WebServiceRef(wsdlLocation = 'http://localhost:8080/com.eviac.blog.jaxwsproj/SampleWSService?wsdl')
 private static SampleWSService Samplews;

 public static void main(String[] args) {
  SampleWSClient wsClient = new SampleWSClient();
  wsClient.run();
 }

 public void run() {
  Samplews = new SampleWSService();
  SampleWS port = Samplews.getSampleWSPort();
  System.out.println('multiplication Result= '+ port.multiply(10, 20));
  System.out.println('Addition Result= '+port.sum(10, 20));
 }

}

3.Right click on the project and click on Run As -> Java Application. This will result following.

multiplication Result= 200
Addition Result= 30

Reference: Getting started with JAX-WS from our JCG partner Pavithra Siriwardena at the EVIAC blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ 4 = eleven



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books