Build SOAP service with JAX-WS

JAX-WS is an API used for building and consuming XML web services in Java, it was first released in Java EE5.

In this tutorial, we provide a step-by-step guide on how to build SOAP web service with JAX-WS and finally publishing it using Endpoint built-in class.

Prerequisites:

  • Eclipse IDE (Neon release)
  • Java 1.8

1. Create standalone Java project

Open eclipse, then select Java -> New -> Java Project

Name your project as JAXWSSoapServiceUsingEndpoint  then click Finish.

2. Create service endpoint

The first step is to create an interface which exposes the different methods provided by our web service, so we create a new class called HelloWorldService under com.programmer.gate package.

Following are the annotations used to make our interface act as a service endpoint:

  • @WebService: this annotation defines our interface as a service endpoint.
  • @SOAPBinding: this annotation defines the style of the request body messages, there are 2 types: RPC & Document, the default style is Document. The main difference between them is that with document style , the request body can be fully validated using XML parser, however RPC style messages are wrapped with a wrapper element. Document styles are recommended for invoking operations with complicated requests/responses.
  • @WebMethod: this annotation exposes our method sayHello() as a web service operation.

3. Create service implementation

The next step is to define the implementation class which encapsulates our business logic, so we create a new class called HelloWorldServiceImpl under com.programmer.gate.

In order to mark our class as an endpoint implementation, we:

  • Let the class implements the HelloWorldService interface.
  • Annotate the class with @WebService
  • Define the exact path of the endpoint interface under endpointInterface attribute.

4. Publish the service

Now that we build our very simple SOAP service with the help of JAX-WS, our final step is to publish it and make it accessible to external environments.

For this purpose, we use Endpoint class provided by JAX-WS.

This class publishes our service to the built-in http server provided by the JRE without the need for any external application/web servers.

Our publisher main class looks like the following:

Here we publish our service and make it accessible through “http://localhost:9090/soap/hello” and define HelloWorldServiceImpl to parse the requests recieved by the service.

5. Test the service

After publishing the service and running the main class, the service wsdl can be accessed through “http://localhost:9090/soap/hello?wsdl”.

It is worth to mention that in order to test the service, you should create a client application or use any web service client tools like: SoapUI.

Here below is a screenshot from SoapUI:

JAXWS Soap

 

 

 

 

 

 

 

 

husseinterek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar