Friday, January 15, 2010

Creating JBossWS Web Services with JBoss 4.2.3GA and JDK1.5

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:

  1. Intermediate knowledge of Java, some experience with XML would help, though not necessary.
  2. Basic Know-how of a web-application
  3. 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:

jbossws initial project structure

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.
jbossws webxml

Lets see how our Java Class which contains the web service would look like:

jbossws helloworld
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.
jbosswss build

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:

jbossws Services console

Lets also have a look at a snippet of generated WSDL, though explaining this is out of scope of this entry.

jbossws wsdl

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.

jbossws client

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:

jbossws final project structure
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:

Mahesh Radhakrishnan said...

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?

Unknown said...

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.