Creating Webservice using JBoss and Eclipse Europa

SHARE & COMMENT :

This article will introduce you to JBoss Webservice(JAX-WS). Here you will be knowing how to use the annotations and create a web service to deploy in JBoss. Since JBoss comes with JAX-WS jars inbuilt, we will not be adding any jars in addition for this article. This article will also give you a example code and walk you through the code. This will also explain creation of dynamic web project (not elaborated) and configuration of JBoss server to it.

also read:

You can also get JBossws download separately and create a webservice and deploy it in your preferred application server. Since we are using JBoss for this article we will not be covering this information here.

Software Used in Configuring JBossws

  • JBoss Application Server 4.0.5.GA.
  • Eclipse Europa (WTP all in one pack)
  • JDK 1.5.x

Pre-Requirements to Learn JBossWs and follow this article.

  • Should have Java knowledge.
  • Should know how to use Eclipse. (Creating web projects in Eclipse)
  • Should have basic knowledge of webservice.

Where to get the JBossWs from?

You can download the software from the following URL:

Defining JBoss server in Eclipse

First thing what you have to do is to define JBoss server in Eclipse. The steps below will explain how to define JBoss server in Eclipse.

  • Step 1 : Open Eclipse WTP all in one pack in a new work space.
  • Step 2 : Change the perspective to J2EE Perspective if it is not currently in J2EE Perspective.
  • Step 3 : Once the Perspective is changed to J2EE, you can see a tab called Servers in the bottom right panel along with Problems, Tasks, Properties.
  • Step 4 : If the Servers tab is not found. Go to Eclipse menu : Windows > Show view and click on Servers, so that Server tab will be displayed.
  • Step 5 : Go to Servers tab window and right click the mouse. You will get a pop up menu called “New”.
  • Step 6 : Clicking on the New menu you will get one more pop up called “Server”. Click on it.
  • Step 7 : Now you will get Define New Server Wizard.
  • Step 8 : In the wizard there are options to define many servers. One among them is JBoss. Click on JBoss and Expand the tree.
  • Step 9 : Select JBoss v 4.0 and click next.
  • Step 10 : Now give the JDK directory and JBoss home directory. Click Next.
  • Step 11 : Now the wizard will show you the default Address, port, etc., Leave it as it is and click on Next.
  • Step 12 : Click on finish.
  • Step 13 : Now you can see the JBoss server listed in the Servers window and the status is Stopped.
  • Step 14 : JBoss server is now defined in Eclipse now and its ready to use from with in Eclipse IDE.

Creating a Dynamic Web Application Project

Now it is time to create a web application in order to Expose a method as a Web service.

Create a Dynamic Web Application Project in eclipse by selecting the JBoss server what we have defined in the Eclipse IDE as the default server for the project. (We assume that who ever is reading this article knows how to create a dynamic web application in Eclipse, So that part is not detailed out here).

Once the JBoss server is selected as the server for the web applications. All the libraries existing in JBoss will be selected and used by eclipse in the Build Path. So no need to add any extra jar files for our work.

Now we will start with a Java code:

This is a simple Java code and does not have any thing to do with Webservices.

JBossWs Code sample without annotations: (TestWs.java)

Our Java code will have a single method called “greet”. Its functionality will be just to accept a string and return the same prefixed with “Hello”.

package com.test.dhanago;

public class TestWs
{
   /**
    * This method will accept a string and prefix with Hello.
    *
    * @param name
    * @return
    */
   public String greet( String name )
   {
      return "Hello" + name;
   }
}

We will add annotations to the above code and modify the code like below:

JBossWs Code sample with annotations: (TestWs.java)

package com.test.dhanago;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * This is a webservice class exposing a method called greet which takes a
 * input parameter and greets the parameter with hello.
 *
 * @author dhanago
 */

/*
 * @WebService indicates that this is webservice interface and the name
 * indicates the webservice name.
 */
@WebService(name = "TestWs")
/*
 * @SOAPBinding indicates binding information of soap messages. Here we have
 * document-literal style of webservice and the parameter style is wrapped.
 */
@SOAPBinding
   (
         style = SOAPBinding.Style.DOCUMENT,
         use = SOAPBinding.Use.LITERAL,
         parameterStyle = SOAPBinding.ParameterStyle.WRAPPED
    )
public class TestWs
{
   /**
    * This method takes a input parameter and appends "Hello" to it and
    * returns the same.
    *
    * @param name
    * @return
    */
   @WebMethod
   public String greet( @WebParam(name = "name")
   String name )
   {
      return "Hello" + name;
   }

}

JBossWs annotations Walk Through

@WebService(name = "TestWs")

Here, @WebService Indicates that this is a webservice class. name = “TestWs” Indicates the webservice name.

@SOAPBinding
   (
         style = SOAPBinding.Style.DOCUMENT,
         use = SOAPBinding.Use.LITERAL,
         parameterStyle = SOAPBinding.ParameterStyle.WRAPPED
    )

Here, @SOAPBinding Indicates binding information of soap messages. The properties below them indicates the style of web service, Here it is document-literal style. And parameter style is Wrapped.

Here, @WebMethod Indicates this is a method exposed as web service@WebParam Indicates the parameter name to be used in soap message.

JBossWs Deployment Descriptor

Once the code is ready and compiled. You have modify the web.xml file located in WEB-INF folder.

Modify the web.xml file like below. (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>TestWS</display-name>
    <servlet>
        <servlet-name>TestWs</servlet-name>
        <servlet-class>com.test.dhanago.TestWs</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestWs</servlet-name>
        <url-pattern>/TestWs</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Deploying the JBoss web service application

Once this is done, its time to build and deploy the application in JBoss Application server.
Once every thing is compiled with out any errors. And if you have enabled the Auto build unctionality of the Eclipse IDE, You have already done with building the application. If the auto build functionality of eclipse is not enabled, then right click on the project and build the project using build option.

Go to Servers window and right click on the JBoss server listed over there and select Run.
Wait for server to start. Once it starts, right click on the server listing. You can find a option called “Add and Remove Project”. Click on the option. You will get a wizard where you can select your projects to move to right and configure with server. Once you moved your project. Click on finish.

Once it is done, you can find that the project is again build and moved to server default deployment folder automatically.

Console will display you like below.

Buildfile: D:\ec2\eclipse\plugins\org.eclipse.jst.server.generic.jboss_1.5.102.v20070608\buildfiles\jboss323.xml
deploy.j2ee.web:
      [jar] Building jar: D:\validation\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\Tws.war
     [move] Moving 1 file to D:\MyBoss\jboss-4.0.5.GA_ws121\server\default\deploy
BUILD SUCCESSFUL
Total time: 10 seconds

The dynamic web application i created is with the name “Tws”. So the build has created Tws.war and moved it to the default deploy folder of the JBoss server.

To make sure web service is started once it is deployed in the JBoss console you can find the log like below.

13:57:52,306 INFO  [ServiceEndpointManager] WebService started: http://:8080/Tws/TestWs

To view the WSDL follow the link http://<machine name>:8080/Tws/TestWs?wsdl

To see the list of webservices deployed in your JBoss Application server follow the link http://localhost:8080/jbossws. This browser console will have link to see your deployed webservices and their WSDL files.

JBossWs Browser Console.

Clicking on View a list of deployed services will list you the deployed web services. In our case we will get the following screen where we can see the registered service endpoints.

Here in this screen you can see the ServiceEndpointAddress link which will take you to the WSDL file.

You can also find the WSDL file in the following path:
<jboss_path>\server\default\data\wsdl\<project_name>.war\<filename>.wsdl

You can generate the client stubs using this file and access the web service. Creating the client stubs to access the web service is out of scope of this article.

The WSDL file generated using JBossWs is shown below:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="TestWsService" targetNamespace="http://dhanago.test.com/" xmlns:tns="http://dhanago.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <xs:schema targetNamespace="http://dhanago.test.com/" version="1.0" xmlns:tns="http://dhanago.test.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="greet" type="tns:greet"/>
   <xs:element name="greetResponse" type="tns:greetResponse"/>
   <xs:complexType name="greet">
    <xs:sequence>
     <xs:element minOccurs="0" name="name" type="xs:string"/>
    </xs:sequence>
   </xs:complexType>
   <xs:complexType name="greetResponse">
    <xs:sequence>
     <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
   </xs:complexType>
  </xs:schema>
  </types>
  <message name="TestWs_greet">
    <part name="greet" element="tns:greet"/>
  </message>
  <message name="TestWs_greetResponse">
    <part name="greetResponse" element="tns:greetResponse"/>
  </message>
  <portType name="TestWs">
    <operation name="greet" parameterOrder="greet">
      <input message="tns:TestWs_greet"/>
      <output message="tns:TestWs_greetResponse"/>
    </operation>
  </portType>
  <binding name="TestWsBinding" type="tns:TestWs">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="greet">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="TestWsService">
    <port name="TestWsPort" binding="tns:TestWsBinding">
      <soap:address location="http://EC3-NOR-124251:8080/Tws/TestWs"/>
    </port>
  </service>
</definitions>

Summary

This article is just a quick start to start with for developers who want to quickly proceed with JBoss web services. It is up to the developers interest to leverage on this and proceed further. This is not the only procedure to expose a web service in JBoss. There might be lot of ways to do that and this is one of the way. So don’t stop here and continue Exploring.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better