DEV Community

lou
lou

Posted on

17 1

SOAP web service with JAX-WS

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

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

import lombok.*;
import java.util.Date;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class StarwarsCharacter {
private int Id;
private String Name;
private Date joinDate;
}

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>

Enter fullscreen mode Exit fullscreen mode

Our class should look like this:

import jakarta.jws.WebMethod;
import jakarta.jws.WebParam;
import jakarta.jws.WebService;
import java.util.Date;
import java.util.List;
//POJO
@WebService(serviceName = "StarwarsWS")
public class StarwarsService {
@WebMethod(operationName = "StarwarsCharacter")
public StarwarsCharacter getStarwarsCharacter(@WebParam int id,@WebParam String name){
return new StarwarsCharacter(id,name,new Date());
}
@WebMethod(operationName = "StarwarsCharacterList")
public List<StarwarsCharacter> starwarsCharacterList(){
return List.of(new StarwarsCharacter(1,"Obi-One",new Date()),
new StarwarsCharacter(1,"Anakin",new Date()),
new StarwarsCharacter(1,"Padmé",new Date()));
}
}

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.

public class ServerJWS {
public static void main(String[] args) {
String address="http://0.0.0.0:8081/";
Endpoint.publish(address, new StarwarsService());
System.out.println("Address : "+address);
}
}
view raw ServerJWS.java hosted with ❤ by GitHub

To access the wsdl :

http://localhost:8081/StarwarsWS?wsdl

Testing the webservice

Link to download SoapUI:

Add a new SOAP project as follow
Image description

To test the first method make a request and pass it your arguments

<arg0>1</arg0>
<arg1>JARJAR</arg1>
Enter fullscreen mode Exit fullscreen mode

The result:

Image description

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

Image description

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
Now click on the project name and go to help > Actions

Image description

Search for Generate Java code from Wsdl

Image description

Image description

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

Image description

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

import proxy.StarwarsService;
import proxy.StarwarsWS;
public class ClientWS {
public static void main(String[] args) {
StarwarsService stub= new StarwarsWS().getStarwarsServicePort(); //middleware to communicates with the ws
System.out.println(stub.astarwarsCharacter(1,"JARJAR").getName());
}
}

Result is as follows :

Image description

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
itsonly_youuu profile image
Abdullah Malik

I follow your complete blog for creating soap api, but i am getting 404 not found on soapUI tool, wsdl file is not generating, I think you missed something related to configuration like something in web.xml? I will be thankful if you check it and provide me solution. Thanks in advance.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs