corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

JAX-WS Web Service Tutorial for JBoss

The JBoss Application Server (AS) 4.0.3 introduced the EJB 3.0 programming model, which made it possible to create and consume JAX-WS web services on JBoss. Out of the box, NetBeans IDE 5.5 does not support JAX-WS web services on JBoss 4. Howewer, this tutorial will show you how to create and deploy a web service using some workarounds and then how to create the web service client and consume the web service. For information about EJB 3.0 and JAX-WS support on JBoss, go here.

Expected duration: 15 minutes

Software Needed for the Scenario

Before you begin, you need to install the following software on your computer:

  • NetBeans IDE 5.5 (download).
  • Java Standard Development Kit (JDK) version 5.0 (download). Do not use JDK 1.6 for theJBoss Application Server or IDE, because JBoss does not support this yet!
  • JBoss Application Server version 4.0.4. Install it as custom installation and select "ejb3" Install type. (download)


How to create JAX-WS Web service


Installing and Configuring the Tutorial Environment

Install NetBeans IDE 5.5 and the JBoss Application Server. If you are working in Windows, you could use automatic installation tool on the dovnload page. Click on Download beside version 4.0.4 and on the next page select the JAR file vith -patched suffix. Confirm the installation location and download the file. Run the JAR file from the command console with command "java -jar [downloaded filename]". The Java Web Start installer will start on your computer. In the installer, select ejb3 Install type. This will install JBoss with EJB3 support, which will allow you to create web services in EJB modules. When the JBoss Application Server is installed, run the IDE and register the JBoss Application Server by right clicking the Servers node in Runtime window.

Creating web service and client in Web module

NetBeans IDE 5.5 supports only JAX-RPC web services on JBoss. JAX-WS isn't supported, although there is a way,how to do it-with JBoss 4.0.4 . JBoss 4.0.4 has implemented only EJB3 support.By default we can't create JavaEE 5 Web module on JBoss.Hovewer, if we create web module for example on Tomcat, we could then change JBoss Application Server in Properties|Run and it will work.

Creating web service in Web module

  1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web category. Click Next. Name the project WSWebModule.
  2. Select Bundled Tomcat(5.5.17) from the combo box and uncheck Set Source Level to 1.4 checkbox
  3. Click Finish.

  4. Right-click the WSWebModule node and choose New > Web Service.
  5. Write JBossWebWS into Web Service Name.

  6. Type org.me.wss in Package, and click Finish.

    The Projects window displays the new web service under Web Services node:

  7. Right-click the WSWebModule node and choose Properties.
  8. Select Run and change the server from Bundled Tomcat to JBoss Application Server 4.
  9. Select Libraries and uncheck JAX-WS 2.0 checkbox .
  10. Click OK.

Deploying web service on JBoss

Now we have created web service but we're not ready to deploy it. Web service is created empty by default,
but in order to deploy it,we must create at least one operation inside the service.

  1. Go to editor, JBossWebWS.java should be opened there.
  2. Right-click inside service body and select Web Service | Add Operation
  3. In next window change "operation" with "greet" ,click on Add, write s into Name and click OK in both windows.
  4. Now the operation should look like this :
                    @WebMethod
                    public String greet(@WebParam(name = "s") String s){
    
                            return null;
                    }
                    
  5. Change the code this way :
                    @WebMethod
                    public String greet(@WebParam(name = "s") String s){
                            return "Hello" + s;
                    }
                    
  6. We must also modify service endpoint according to JSR-181 specification,because otherwise it couldn't be accessed from outside. Go to Projects tab, open Configuration Files node and select web.xml. Double-click on it and it'll open in editor. Click on XML in command bar.
    You'll see the code:
                            <?xml version="1.0" encoding="UTF-8"?>
    
                            <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
                                <listener>
                                    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
                                </listener>
                                <servlet>
                                    <servlet-name>JBossWebWS</servlet-name>
    
                                    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
                                    <load-on-startup>1</load-on-startup>
                                    </servlet>
                                <servlet-mapping>
                                    <servlet-name>JBossWebWS</servlet-name>
    
                                    <url-pattern>/JBossWebWS</url-pattern>
                                </servlet-mapping>
                                <session-config>
                                    <session-timeout>
                                        30
                                    </session-timeout>
    
                                </session-config>
                                <welcome-file-list>
                                    <welcome-file>
                                        index.jsp
                                    </welcome-file>
                                </welcome-file-list>
                            </web-app>
    
    
                             
    Change the code to this form:
                            <?xml version="1.0" encoding="UTF-8"?>
    
                            <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
                                <servlet>
                                    <servlet-name>JBossWebWS</servlet-name>
                                    <servlet-class>org.me.ws.JBossWebWS</servlet-class>
                                    <load-on-startup>1</load-on-startup>
                                    </servlet>
                                <servlet-mapping>
                                    <servlet-name>JBossWebWS</servlet-name>
    
                                    <url-pattern>/JBossWebWS</url-pattern>
                                </servlet-mapping>
                                <session-config>
                                    <session-timeout>
                                        30
                                    </session-timeout>
    
                                </session-config>
                                <welcome-file-list>
                                    <welcome-file>
                                        index.jsp
                                    </welcome-file>
                                </welcome-file-list>
                            </web-app>
                         

  7. Click File and Save all.
Now it's OK, so right click on Web module and select Deploy Project. Service will be deployed on the JBoss Application Server. If you Right-click on web service's node in Projects tab, you'll see, that there's not Test Web Service option. We can still display in web browser wsdl code of service or even the tester, like we know from Bundled Tomcat. Right-click on web service's node and select Properties.In next window select service's URL address, as displayed here and copy it(Ctrl+C):


Go to web browser and Paste(Ctrl+V) URl into address field. If you now press Enter, WSDL code of our web service will be displayed.


Creating web service client in web module

  1. Choose File > New Project (Ctrl-Shift-N). Select Web Module from the Web category. Name the project WSCWebModule.
  2. Select Bundled Tomcat(5.5.17) from the combo box and uncheck Set Source Level to 1.4 checkbox
  3. Project folder can't have spaces in path,othervise client creation fails.Change project folder,if neccessary.

    Click Finish.

  4. Choose File > New File (Ctrl-N). Select Web Service Client from the Web Service category and click Next.

    Let selected default Project and click on button Browse to the right.
    Click on WSWebModule ,select JBossWebWS and click OK.
    Type org.me.wsc in Package, and click Finish.

  5. The Projects window displays the new web service client under Web Service References node:


Creating web service and client in EJB module

Within NetBeans IDE 5.5, you can only deploy JAX-RPC web services to the JBoss Application Server. JAX-WS is not supported, although there is a way how to do it-with JBoss 4.0.4 . JBoss 4.0.4 has implemented EJB3 support, hovewer, so we can work also with JAXWS.

Creating web service in EJB module

  1. Choose File > New Project (Ctrl-Shift-N). Select EJB Module from the Enterprise category. Name the project WSEJBModule.
  2. Select JBoss Application Server 4 from the combo box.
  3. Click Finish.

  4. Right-click the WSEJBModule node and choose New > Web Service.
  5. Write JBossEJBWS into Web Service Name..

  6. Type org.me.ejbwss in Package, and click Finish.

    The Projects window displays the new web service under Web Services node:


Deploying web service on JBoss

We have created web service but we're not ready to deploy it. Web service is created empty by default,
but in order to deploy it,we must create at least one operation inside the service.

  1. Go to editor, JBossEJBWS.java should be opened there.
  2. Right-click inside service body and select Web Service | Add Operation
  3. In next window change "operation" with "greet" ,click on Add, write s into Name and click OK in both windows.
  4. Now the operation should look like this :
                    @WebMethod
                    public String greet(@WebParam(name = "s") String s){
    
                            return null;
                    }
                    
  5. Change the code this way :
                    @WebMethod
                    public String greet(@WebParam(name = "s") String s){
                            return "Hello" + s;
                    }
                    
  6. We're ready to deploy it. Right click on EJB module and select Deploy Project. Service will be deployed on the JBoss Application Server. If you Right-click on web service's node in Projects tab, you'll see, that there's disabled Test Web Service option. We can still display in web browser wsdl code of service but not the tester, like we know from Bundled Tomcat or glassfish. Right-click on web service's node and select Properties.In next window select service's URL address, as displayed here and copy it(Ctrl+C):



    Go to web browser and Paste(Ctrl+V) URl into address field.Hovewer, there's bug in the URL we just pasted. We must change it from http://localhost:8080/JBossEJBWS?wsdl to http://localhost:8080/WSEJBModule/JBossEJBWS?wsdl. Now press Enter and wsdl code of deployed service will be displayed in your web browser.


    Creating web service client in EJB module

    1. Choose File > New Project (Ctrl-Shift-N). Select EJB Module from the Enterprise category. Name the project WSCEJBModule.
    2. Select JBoss Application Server 4 from the combo box and uncheck Set Source Level to 1.4 checkbox
    3. Project folder can't have spaces in path,othervise client creation fails.Change project folder,if neccessary.

      Click Finish.

    4. Choose File > New File (Ctrl-N). Select Web Service Client from the Web Service category and click Next.

      Select WSDL URL and enter "http://localhost:8080/WSEJBModule/JBossEJBWS?wsdl".
      Click on button Browse to the right.
      Click on WSEJBModule ,select JBossEJBWS and click OK.
      Type org.me.ejbwsc in Package, and click Finish.

    5. The Projects window displays the new web service client under Web Service References node:


    Consuming web service

    1. Go to Projects tab and double click on WSCWebModule > index.jsp file.

    2. Select commented area in page's body,starting with <%-- This example uses JSTL, uncomment the taglib directive above.
      Right-click and select Web Service Client Resources | Call Web Service Operation.
      Click on WSWebModule ,select JBossWebWS | greet and click OK.
    3. Code will look like this :
                  <%@page contentType="text/html"%>
      
                  <%@page pageEncoding="UTF-8"%>
                  <%--
                  The taglib directive below imports the JSTL library. If you uncomment it,
                  you must also add the JSTL library to the project. The Add Library... action
                  on Libraries node in Projects view can be used to add the JSTL 1.1 library.
                  --%>
      
                  <%--
                  <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
                  --%>
      
                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                     "http://www.w3.org/TR/html4/loose.dtd">
      
                  <html>
                      <head>
                          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      
                          <title>JSP Page</title>
                      </head>
                      <body>
      
                      <h1>JSP Page</h1>
      
      
      
      
                      <%-- start web service invocation --%><hr/>
                      <%
                      try {
                          org.me.wsc.JBossWebWSService service = new org.me.wsc.JBossWebWSService();
      
                          org.me.wsc.JBossWebWS port = service.getJBossWebWSPort();
      
                           // TODO initialize WS operation arguments here
                          java.lang.String s = "";
                          // TODO process result here
      
                          java.lang.String result = port.greet(s);
      
                          out.println("Result = "+result);
                      } catch (Exception ex) {
      
                          // TODO handle custom exceptions here
                      }
                      %>
                      <%-- end web service invocation --%><hr/>
                      </body>
      
                  </html>
      
      
    4. Change string 'java.lang.String s = "";' to 'java.lang.String s = "your_name";'.
    5. Right-click the WSCWebModule node and choose Properties.
    6. Select Run and change Server from Bundled Tomcat to JBoss Application Server 4.
    7. Click OK.

    8. Right click into Projects tab on WSWebModule node and select Run Project.

Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems