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.
- jaxb-api.jar
- jbossws-native-jaxws-ext.jar
- jbossws-native-jaxws.jar
- jbossws-native-saaj.jar
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.
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 :-)
2 comments:
Hi,
Your example is very good. But when I try to deploy my WAR, I get the below message and its not getting deployed.
2014-09-05 11:50:35,343 INFO [org.jboss.web.tomcat.service.deployers.TomcatDeployment] (main) deploy, ctxPath=/TestEFT2
2014-09-05 11:50:35,392 WARNING [javax.enterprise.resource.webcontainer.jsf.config] (main) Unable to process deployment descriptor for context '/TestEFT2'
2014-09-05 11:50:35,392 INFO [javax.enterprise.resource.webcontainer.jsf.config] (main) Initializing Mojarra (1.2_12-b01-FCS) for context '/TestEFT2'
Any idea?
Hi Mahesh,
Do you have a more detailed stack trace available in the server logs as to why the container was not able to process the deployment descriptor for TestEFT2 ?
Generally if there are special characters or the web.xml file is not properly formatted or tags are not closed such errors come.
Post a Comment