Building a web service endpoint in Java
Ok, so what is a web service?
I provide an overview of what Web Services (WS) are in this post, An introduction to Web Services. Give it a read and pop back here
Seriously, Java? The most insecure software platform ever?
Yes, Java. The platform agnostic software environment that is taught in a lot of University CS courses and has had a consistently high spot in the tiobe programming language popularity chart (Currently #2, with only .2% less market than the number one, C).
Its also worth bearing in mind that the security issues that have plagued Java in recent months are usually related to the use of a web plug in. This plug in causes issues as Java is a powerful, fully featured platform which when combined with a web plugin introduces a large number of attack vectors. From a Desktop/Server application perspective Java is quite safe and in many ways safer than native code due to its Bytecode interpreting sandboxed process virtual machine.
Which Web Service technology will be used here?
In this example SOAP based web services will be used to provide a quick example. For large production systems you may prefer to look at JSON (Message format) and REST (communication strategy) based WS. This example will be built using Netbeans.
Plug-in and communicate
What??? I’m quite I read a lot so I’m plugged in but I‘m a stereotypical Socially Awkward Penguin, I don’t like the talky talk. Not fair!!!
Not that type of plugged in and communicate, to develop WS in Java we need to install a development plugin. To do this go to the Tools –> Plugins menu.
In the plugin interface go to Available plugins and install the Java web and EE option. After installation restart Netbeans.
Once the WS support is enabled a hosting server is needed, this can be specified or isntaleld from the Tools-> Servers menu. Click Add Server
Next choose a server, I have chosen the Glassfish server below.
If you have the installed already you may specify the path or download the server. To download tick the agreement then select Download Now. Then go through the Installation process. Follow the screen shots below
Now that your environment is ready you can now create a project.
Time to make a project.
Create a New ‘Web Application’ Project as shown below.
Yes, I am aware I forgot the ‘l’, I have another Example Application in the same folder and am a little lazy.
Do not click any of these frameworks as they are not needed for this tutorial.
Under web services click New –> Web Service and then specify customization.
The code in question will go into a class called MathFun and will calculate the Pearson product-movement coefficient. A walkthrough of calculating this is shown here. This code accepts 2 arrays containing Doubles and returns a double as a reply.
Once the code has been added to a class it can then be called by the service endpoint code by specifying a @WebMethod.
This code follows this form.
And we are done!!
Yes, it is that easy to set up a WS. The next step involves consuming it from a device/web browser. This is simple enough as most development environments support SOAP.