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.
- 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.
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.
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
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.
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:
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 :-)