This blog is about developing a simple Java SOAP (Simple Object Access Protocol) web-service using Spring Tool Suite (STS) integrated development environment. I will explain simple steps to develop a web-service and deploy it on application server (ex: Apache Tomcat) and do the integration. For the sample program, I have used the default java web service framework called Axis and used Apache Tomcat 7.0 for the deployment. This blog mainly targets the beginners on Web-Service. Obviously, you should have some programming skill to understand the context of this blog.
- Create new project in STS
Create a new Dynamic Web Project in your workspace under STS.
- Create a package
Create new java package to implement the relevant source files for the web-service. You can specify a name and define your own structure for the new package.
- For the web service, we’ll be needing a java interface and it’s implementation (impl) class. In this example we’ll be having only one web service method. So we create only one interface and its impl class. I have created both of them in a single package which I have created above, but you can define them the way you want. You can create different packages to separate the interfaces and class files. I have used some annotations within interface and impl class. In the next step I have described them in-detail.
In above java resources, you could see that I have used several annotations. Annotations are syntactic metadata that can be added to java classes, methods, variables and resources. For more information about annotations, please refer to this Oracle documentation. I have used the following annotations to create the interface and impl class.
Marks a java class as a Web Service implementation, or a java interface as a Web Service interface.
Defines the mapping of our Web Service onto the SOAP messaging protocol. It actually binds our Web Service object onto the protocol.
The ‘style’ parameter of @SOAPBinding annotation defines the encoding style of the messages that comes-in (inbound) and goes-out (outbound) from the service methods. We have given the RPC encoding style to make sure the message encoding is for the Remote Procedure Calls (RPC).
When this annotation was used, the java method will be exposed as a Web Service operation (servicing method). These methods should be public. And if you didn’t return any values from these methods, there will not be any response messages/values sent back to the client(s) from the server.
With these annotation definitions, now you may have accurately guessed what the web servicing method would be in our java program. Of-course, in our example it is easy to identify since our class has single method. In our application, the web service operational method is sayHello, that accepts a string parameter from the client and returns a string message back to the client from the server.
- Generate the WSDL (Web Services Definition Language)
Now our program is almost ready to be deployed on an application server, but before that we need to generate the noble WSDL file. This is the file that describes our web services and how to access them and about the connection and all. This file was written in XML in a specific format that can be understood by the java runtime.
Note: There are mainly two methods available to create a web service. One is code-first and the other one is contract-first. Here we follow the code-first method, that means we develop our java resources (classes & interfaces) first and then generate the contract (WSDL). In contract-first method, we create the WSDL first and then generate the java sources from it.
Follow these steps to generate the WSDL file in STS : –
Right click on the STS project and select new ⇨ web service from the list.
Click Next button and give your web-service impl class name. Class name should be given with the package representation. It is far better if you can just click the browse button and select your class name.
Click the Next button and select your web service operational method(s) from the list.
Click Next and when ‘Server Startup’ screen appears then click the Cancel button. The Server Startup screen allows you to deploy your application in the default web server within STS. In my case, I will be deploying my project in an external Tomcat server. Due to this, I do not want STS to deploy my project by itself.
Now I have generated the WSDL file for my project.
- Packing every resource
Everything was set to deploy our web-service project. To deploy this on Tomcat server, first we must generate a WAR file (WAR is the Web Archive that contains all your resource files). We can deploy this WAR file in our application server. Then the server will extract this WAR into its application server context. To generate the WAR file in STS, just right click on the project in STS and under the ‘Export‘ menu select WAR. From the export window give the destination location where you want to export your WAR file. Click the Finish button to complete the process.
- Deploy the web service
To deploy the WAR file that we have created above, first run the Apache Tomcat server. This can be done by double clicking the startup.bat file in the Tomcat bin directory. When the server is up & running, navigate to Tomcat app manager portal through your browser http://[server-host]:[server-port]/manager/html. Login using the Tomcat Manager credentials that you have configured in conf file (Configuring Apache Tomcat server is a separate topic to discuss. Check this link to learn Configuring Manager Application Access). Upload your WAR file in the following location. Now we are good to proceed with testing once the deployment has succeeded.
- Our web service is functioning now. The method that we have created can be accessed by any clients (SoapUI, ReadyApi and etc) that supports WSDL based web-services. To test this, we can create our own java or web clients, but instead we also can find lot more web-service testing tools in the marketplace. SoapUI is one such powerful tool that we can use to test our web-service project.
In my next blog, I will be discussing about testing this SOAP web-service project using SoapUI tool.