Continuing from the last post, today we will learn about creating a Simple Web Service using jBossWS (a JAX-WS implementation) and deploy it on JBoss Server. Also, we will see how can we create a WS-Client using in-build utilities and consume the web service with ease.
Prerequisites:
- Intermediate knowledge of Java, some experience with XML would help, though not necessary.
- Basic Know-how of a web-application
- Setting up the required environment.
Getting Started:
Create a New Java Project. Name it anything you like. Create appropriate structure for a web application like we create an output folder named ‘web’ and create ‘WEB-INF’ in it, which will hold the deployment descriptor ‘web.xml’. The initial structure of our web application looks as follows:
With JBossWS, to create a web service is as easy as creating a Java class and exposing a method and create its entry in the deployment descriptor. Lets first create an entry in web.xml for the the EndpointServlet class and configure it for the Java class containing the Web Service implementation as shown below:Here, we specify EndpointServlet (comes bundled with JBossWS stack) which is used to publish/deploy your web service over JBoss. We also provide its mapping giving the url-pattern of the web service.
Lets see how our Java Class which contains the web service would look like:
Notice that we have noticed
JAX-WS Annotations to decorate our class. The
@WebService annotations tells us that this class contains methods exposed by a web service and
@WebMethod annotation points out exactly which method is exposed.
To make sure that your code compiles, you must include the JAR’s that come with JBossWS native stack. Therefore, we add the following JAR’s to the lib folder of WEB-INF and also configure it on the build-path of the project so that you do not get compilation error in Eclipse.
- jaxb-api.jar
- jbossws-native-jaxws-ext.jar
- jbossws-native-jaxws.jar
- jbossws-native-saaj.jar
So, thats all you need to do in order to expose your web service. Lets now publish this web service on JBoss. To do so, we will package it as a WAR file using ANT. I have used the following ANT script to create WAR for my web service.
Now, you can copy this war file to the deploy directory of your JBoss Installation. Restart your server and check the logs to see whether it is deployed successfully or not.
[TomcatDeployer] deploy, ctxPath=/jaxWS,
warUrl=.../tmp/deploy/tmp5621016708388909925jaxWS-exp.war/
[WSDLFilePublisher] WSDL published to: file:/E:/JBoss/jboss-4.2.3.GA
/server/default/data/wsdl/jaxWS.war/HelloWorldService300665606.wsdl
So, that tells you that your web service has been deployed. To double check, you can always check the JBoss web service console on this url:
http://localhost:8085/jbossws/services It shows you a list of all endpoints deployed currently, something like below:
Lets also have a look at a snippet of generated WSDL, though explaining this is out of scope of this entry.
JAX-WS provides supports for all Java in-build data types as well as for complex user-defined objects. The WSDL that is generated on publishing the web service contains all the information about the parameters accepted by the web service and also about the format in which the data is returned. Thus, this WSDL is more of a contract on which the communicating parties must agree before communicating.
Client Generation:
Now, we consume this web service by creating client using utilities provided by JBossWS stack ( Examples include wsconsume, wsget, wsrunclient etc )
>>> wsconsume –k http://localhost:8085/jaxWS/prodsWS?wsdl
wsconsume will parse the WSDL and –k option will generate the java classes which will be used by the client using JAXB framework.
Now, we will create a TestClient (to test our webservice), which will finally consume the web service.
As expected, on running this client, it will print “Hello My Name is Agraj” as output. Please note that in order to run this client successfully, you must add few more client jars to the project.
The final structure of the web application (web service + client) looks like following:
So, that’s all I want to talk about web services today, I hope it was of some help. Please post your views or corrections as comments.
Note: You may encounter a few problems while developing web services using JBossWS and JDK 1.6 since there are some compatibility issues and conflicting jars that create problems. Your best friend in such a case is Google :-)