Create a Java SOAP API JAX-WS
Next, I will explain how to create SOAP web services, or what is the same … Create a Java SOAP API JAX-WS. We will use the common methods to publish the services on our local machine.
What is SOAP? Simple Object Access Protocol. It is a communication protocol, usually via HTPS or JMS, in which the exchange of messages is carried out using an XML format called WSDL (Web Services Description Language).
These types of services are platform and language independent, that is, the server can be developed in JAVA and the client in .NET or PHP.
The library used for SOAP services is JAX-WS: (Java API for XML Web Services).
Create Dynamic Web Project
In Eclipse, select File -> New -> Dynamic Web Project.
Press Finish.
Create classes
Create model
User.java
package es.rosamarfil.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
public static List<User> users = new ArrayList<>(Arrays.asList(
new User("Rosa", "Marfil"),
new User("Pepito", "Grillo"),
new User("Manuela", "Río")));
public String name;
public String username;
public User() {
super();
}
public User(String name, String username) {
super();
this.name = name;
this.username = username;
}
public void setName(String name) {
this.name = name;
}
public void setUsername(String username) {
this.username = username;
}
public static List<User> getUsers()
{
return users;
}
}
Create Interface e implementation
We are going to create an interface with the methods to publish. We will call it SOAPI.java
SOAPI.java (Interface)
package es.rosamarfil.soap;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import es.rosamarfil.model.User;
@WebService
public interface SOAPI {
@WebMethod
public List<User> getUsers();
@WebMethod
public void addUser(User user);
}
SOAPImpl.java (Implementaion)
package es.rosamarfil.soap;
import java.util.List;
import javax.jws.WebService;
import es.rosamarfil.model.User;
@WebService(endpointInterface = "es.rosamarfil.soap.SOAPI")
public class SOAPImpl implements SOAPI{
@Override
public List<User> getUsers() {
return User.getUsers();
}
@Override
public void addUser(User user) {
User.getUsers().add(user);
}
}
Publish services
To do this, we are going to create a “main” class, which is executed when starting our application and publishes the services. For this, the use of the EndPoint class provided by the JAX-WS library is essential.
PublishServices.java
package es.rosamarfil;
import javax.xml.ws.Endpoint;
import es.rosamarfil.soap.SOAPImpl;
public class PublishServices {
public static void main(String[] args) {
/ *
* The services are published through a virtual server.
The port can be any.
Once the application is run, the WSDL contract is published
* /
Endpoint.publish("http://localhost:1516/WS/Users", new SOAPImpl());
}
}
To publish the contract, we run as a java application. Note that there are no errors in the Eclipse console.
In the browser, enter: http://localhost:1516/WS/Users?wsdl and observe the operations that the server makes available through the wsdl.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. -->
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" name="SOAPImplService" targetNamespace="http://soap.rosamarfil.es/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://soap.rosamarfil.es/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<types>
<xsd:schema>
<xsd:import schemaLocation="http://localhost:1516/WS/Users?xsd=1" namespace="http://soap.rosamarfil.es/"/>
</xsd:schema>
</types>
<message name="addUser">
<part name="parameters" element="tns:addUser"/>
</message>
<message name="addUserResponse">
<part name="parameters" element="tns:addUserResponse"/>
</message>
<message name="getUsers">
<part name="parameters" element="tns:getUsers"/>
</message>
<message name="getUsersResponse">
<part name="parameters" element="tns:getUsersResponse"/>
</message>
<portType name="SOAPI">
<operation name="addUser">
<input message="tns:addUser" wsam:Action="http://soap.rosamarfil.es/SOAPI/addUserRequest"/>
<output message="tns:addUserResponse" wsam:Action="http://soap.rosamarfil.es/SOAPI/addUserResponse"/>
</operation>
<operation name="getUsers">
<input message="tns:getUsers" wsam:Action="http://soap.rosamarfil.es/SOAPI/getUsersRequest"/>
<output message="tns:getUsersResponse" wsam:Action="http://soap.rosamarfil.es/SOAPI/getUsersResponse"/>
</operation>
</portType>
<binding type="tns:SOAPI" name="SOAPImplPortBinding">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="addUser">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getUsers">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SOAPImplService">
<port name="SOAPImplPort" binding="tns:SOAPImplPortBinding">
<soap:address location="http://localhost:1516/WS/Users"/>
</port>
</service>
</definitions>
Test the Services through a Client
In Eclipse, create a new Java Project:
Then select the project and select File -> New -> Other -> Web Services -> Web Service Client. Assign the address where the wsdl contract is published. Press Finish.
The following classes are created:
Create a “main” execution class called UserClient.java:
We are going to complete the class to call the services, in such a way that it looks like this:
package es.rosamarfil.client;
import java.rmi.RemoteException;
import java.util.Arrays;
import javax.xml.rpc.ServiceException;
import es.rosamarfil.soap.SOAPI;
import es.rosamarfil.soap.SOAPImplServiceLocator;
import es.rosamarfil.soap.User;
public class UserClient {
public static void main(String[] args) {
SOAPImplServiceLocator locator = new SOAPImplServiceLocator();
try {
SOAPI userService = locator.getSOAPImplPort();
//Se muestra la lista de usuarios
System.out.println("Lista de usuarios: n" + Arrays.toString(userService.getUsers()));
//Se añade nuevo usuario
userService.addUser(new User("Pablo","Ruiz"));
//Se muestra la lista de usuarios
System.out.println("Lista de usuarios: n" + Arrays.toString(userService.getUsers()));
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Console output:
To show the data of each user, the toString() method of the class is.rosamarfil.soap.User has been overwritten:
@Override
public String toString() {
return "User [name=" + name + ", username=" + username;
}
And that’s all!! This is a simple way to start creating SOAP JAVA services and creating a SOAP Java API with JAX-WS.
Remember that it is also possible to create web services with other technologies, such as REST. Although each technology can be used for specific purposes. If you are interested in how to create a REST API, you can see this article: Create a Java REST API.
I hope it has been useful to you and see you in the next tutorial.
Share if you liked it !!