Build SOAP Web Service for Millennial Java Developer

Yes, because you stuck with old legacy third-party system

Image courtesy of pexels

Are you proclaimed as millennial Java developer who play with the newest lambda expressions, working on micro services architecture, experienced in building REST web services and even you know about Hypermedia as the Engine of Application State (HATEOAS). Then, the nightmare is come. You need to integrate your system with old third-party system, and of course they don’t have fancy things like REST. They offer you Simple Object Access Protocol (SOAP) to communicate with their system.

Yeah, sometimes your application is not only consumes by human but a system or to be precise a machine. Integrate between system is already common, for an old school programmer they call it Enterprise Application Integration (EAI), moving into 20th century you will be heard the term Service Oriented Architecture (SOA), and the funny things is, now as millennial developer we called it Microservices.

Okay, there is some method for each application to communicate. Communication method that still exist until now was REST and SOAP, but there is still many method such as Remote Method Invocation (RMI), Common Object Request Broker Architecture (CORBA), and XML-RPC. Ok, I admit even I don’t know the last three method. I, like you, mostly working with REST and just recently I need to working to integrate my system using SOAP.

The best part being Java developer is you could easily build SOAP Web Service. Standardized in Java Specifications Request (JSR) 224 or more known as JAX-WS. You will be able build client-server SOAP web service with only Java EE without any external dependencies and no, this won’t be complicated tutorial since you will use Java EE 7. Since Java EE 5 everything become easier you don’t need to write cumbersome xml configuration or write boilerplate code and in Java EE 7 everything is just convention over configuration. This will required JDK version 8, Apache Maven, Payara as application server and Javaee7 Essentials Archetype.

SOAP Server Side

You couldn’t play with SOAP web service if you don’t have the producer or to be precise the server side, right? So, the first steps is you need to build your own SOAP web service. Of course this is for the sake of learning, in real world you just need to communicate with existing service. But, hey who knows maybe you will experienced to build your own SOAP web service 🙂

The service is pretty simple, it will receive request with customer credentials such as username and password. To be able passed the authentication the username must be swhp (yeah because it was me 🙂) and no the password is not important just the username.

Ok, now you already know what is the business process. Let’s move to write the entity class for Account. Since you want more readable and explanatory response you could write Status entity plus with the code and description about the response.

entity class for Account
entity class for Message response
entity class for Status that related to Message entity

Ok, you almost done. Now, you need to build class to become the endpoint for your SOAP web service.

Done, yes congratulations you already build your own SOAP web service. execute on command line mvn clean install deploy your Web Archive (war) project into Payara then open this URL: http://[your_host]:[your_port]/[your_app_name]/AccountService?wsdl and you will see the Web Service Description Language (WSDL). Actually, the WSDL not shown on my Firefox but shown properly on Chrome.

WSDL from Account Service

SOAP Client Side

Mostly you’re working on client side, building system that need to consume the SOAP web service. Nowadays is very rare to build server side with SOAP since almost millennial programmer prefer REST, right? SOAP is just for old system, but this old system is already live long enough to reach maturity level and widely used by many client and you’re one of this client. So, you need to build your system to be able communicate trough SOAP.

Before you could build a client side, you must know or have the WSDL. WSDL is more like documentation from the API. Just imagine you’re working with third-party system and they have SOAP to communicate, the first thing you need is to asking or looking their WSDL. Why? because you need that to generate the java files for your project. Sounds weird? maybe, but this is how SOAP working as far as I know and I’m open if there is another way to create a SOAP client.

You can easily generate java files from WSDL using wsimport. Wsimport is already provided in JDK so you just need to execute this command line on your client directory.

wsimport -keep -verbose http://[your_host]:[your_port]/[your_app_name]/AccountService\?wsdl -d src/main/java

Make sure you already deploy the SOAP server side project so the wsimport can generate the java files on src/main/java and you will see that the files was on package id.swhp.account just like the namespace in SOAP server side, so if you want the files stored in your desired package automatically just change the namespace on SOAP server side. Don’t forget to delete .class files since you just need the java files.

Knowing your objection is to validate the user credentials trough SOAP web service, first you need to write the validation class that use the generated artifact from WSDL.

Both AccountService and AccountService_Service is artifact from generated WSDL. The purpose is simple, call the SOAP web service and execute method inqAccount(). Now, you need to create your own entity for User on your client project.

The last, you need to create an EJB class as an interface either you want to build a website with JSF or build a REST with JAX-RS.

The Conclusion

You know, I just need to spend 2 hours to build this simple client-server SOAP web service with Java EE. Java already easier the process in making SOAP, you as developer just need to focus on business process. As always you could see the full code on my github, and as millennial developer I’m open to any advice from experienced SOAP developer about how to build a good SOAP web service.

If you enjoyed reading this article, please recommend with click the 👏 below and share it to help others find it!

References: