SOAP web service with JAX-WS
Tags: java, webservice, soap, xml
Published: 2022-11-20 18:41:16

In tutorial you will learn to publish and consume SOAP based JAXWS webservice using maven

##  What is Soap 

SOAP stands for Simple Object Access Protocol, it's a transport protocol based on XML and HTTP. It sends and receives requests and responses.

##  What is WSDL 

WSDL or Web Services Description Language is an XML notation for describing a web service.

## What is JaxWS

JaxWS stands for Java API for XML Web Services. It builds web services and clients that communicate using XML.

## What is SoapUI

SoapUI is an API Testing Tool, it is used for RESTful Web Services or HTTP based services as well as SOAP Web Services.


## Here is an overview of what we're going to need for this project:

1. - A web service 
2. - A JAXWS server
3. - SoapUI for testing purposes 
 

## To start create a Maven project :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hab9ccinlyq1ojt4rlmf.png)

Add a new java class, let's call it StarwarsCharacter

{% embed https://gist.github.com/leriaetnasta/4474e8b928b3a7f194c5f71cf01159fa %}

Now add another java class and let's call this one StarwarsService.

Within this class declare these 2 methods:

 - getStarwarsCharacter(): returns a new StarwarsCharacter
 - starwarsCharacterList(): returns a new list of StarwarsCharacters

This class is going to serve as our Webservice endpoint. In order to do that annotate it with the @Webservice annotation. And to expose its 2 methods to web service clients we're going to annotate each of them with the @WebMethod annotation. 

But before we can do any of that add this dependency to you POM.XL file:

```
<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-ri -->
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-ri</artifactId>
    <version>4.0.0</version>
    <type>pom</type>
</dependency>

```

Our class should look like this:

{% embed https://gist.github.com/leriaetnasta/e7e233846a0d537098e9b6d38b81164c %}

Now let's deploy our webservice

Add a Server class that will publish the webservice endpoint to accept incoming requests using the method publish()

This method takes as arguments: the http server address and an instance of the webservice.

It return an http server that runs on the port passed in the address parameter and it is used to access the webservice passed as the implementor parameter.

{% embed https://gist.github.com/leriaetnasta/708db2c2de8b69706cd5b5cff1350908 %}

To access the wsdl : 
`
http://localhost:8081/StarwarsWS?wsdl
`

## Testing the webservice

Link to download SoapUI:
{% embed https://www.soapui.org/downloads/soapui/ %}

Add a new SOAP project as follow
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ig4pifcvoclbgkhvme7.png)

To test the first method make a request and pass it your arguments 
```
<arg0>1</arg0>
<arg1>JARJAR</arg1>
```
The result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/soqvcgjbqf9qh4ul2nfm.png)

On to the next method, this one doesn't take any arguments:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgwmpax3t8jm6616nrhz.png)

## Consume this SOAP webservice

Create a new project and add the JAX dependency to the POM.XML file

Before we can proceed you need to install this plugin 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohm752xn0o8u87w49u1d.png)
Now click on the project name and go to help > Actions

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcn7aue2g7no4lcr969e.png)

Search for Generate Java code from Wsdl

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6w70us9ecu8yq5v25tku.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70ct9c6qhj86nnoyxwbw.png)

It's going to generate the java classes inside your package

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rg97ri6rk84ebnyqgh9x.png)

Add a new class that's going to use a middleware to consume your webservice


{% embed https://gist.github.com/leriaetnasta/8c0deae3a9e1baa30714e09b113a6af6 %}


Result is as follows :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz7vtk0sy5nrr0j0otpt.png)