JAXWSNB6Maven2GlassFishV2

JAX-WS Web Services in a Java EE 5 Web Application (using Netbeans 6, Maven2, and GlassFish V2)

Contributed By: Łukasz Budnik


[[{TableOfContentsTitle=TableOfContents} | {TableOfContents title='Table of Contents'}]]


Introduction


If we deploy our applications on a Java EE 5 compliant server, we don't need to use any external engines in order to expose parts of our functionality as Web Services – Java EE 5 compliant servers have it all.

Throughout this tutorial, we will be developing a simple Jave EE 5 Web Application which will expose some of its functionality as a JAX-WS Web Service.

We will be using NetBeans 6, Maven2, and GlassFish V2.

This tutorial assumes you have a basic knowledge of JAX-WS technology and Maven2. Also it is assumed that you have Maven plugin already installed.

Let's get started!

Beginning Project


  1. Open NetBeans 6 and create a new project. In
    New Project
    window select {Maven Project}:http://wiki.netbeans.org/attach/JAXWSNB6Maven2GlassFishV2/nb_maven2_JAXWSNB6Maven2GlassFishV2.png
  2. Click
    Next
    . The Maven Archetype window opens.
  3. In the Maven Archetype window select
    Maven Webapp Archetype
    :http://wiki.netbeans.org/attach/JAXWSNB6Maven2GlassFishV2/nb_maven2_archetype_JAXWSNB6Maven2GlassFishV2.png
  4. Click Next. The Name and Location window opens.
  5. In the Name and Location window, name the project JAXWSWebService and accept the default options for the other values.

Amending project settings


Standard Maven archetypes are pretty out of date, and you need to update the version used by the project.

  1. After the project is created, right click it and select
    Properties
    .
  2. In the
    Sources
    tab, select source level of at least {1.5}, and make sure that the encoding is set to UTF-8.

Writing simple JAX-WS Web Service


  1. Create a new package, call it
    org.xh.studies.jaxws.webapp
    .
  2. Create the
    Math
    class in the new package.
  3. Copy and paste the following code into
    Math
    :{

@WebService public class Math {

 @WebMethod
 public int add(int a, int b) {
     return a + b;
 }

} </pre>

  1. Add import statements for
    javax.jws.WebService
    and <tt>{javax.jws.WebMethod
    }.
  2. Save this file.

Deploying the project


Run the project directly from NetBeans (shortcut key:
[F6]
).

There will be an error:

[ERROR]No suitable Deployment Server is defined for the project or globally.
[ERROR]Mojo:
[ERROR]    org.codehaus.mevenide:netbeans-deploy-plugin:1.2.4:deploy
[ERROR]FAILED for project:
[ERROR]    org.xh.studies.jaxws.webapp:JAXWSWebApp:war:1.0-SNAPSHOT
[ERROR]Reason:
[ERROR]No suitable Deployment Server is defined for the project or globally.
To fix this error, you need to specify the application server on which you deploy the
.war
file. You also need to specify the relative so that it includes the service. To specify the server and relative URL, perform these steps:
  1. Right-click the project and select
    Properties
    . Navigate to the {Run} tab.
  2. In the Server field select GlassFish V2, and in the Relative URL field, type /MathService.http://wiki.netbeans.org/attach/JAXWSNB6Maven2GlassFishV2/nb_server_JAXWSNB6Maven2GlassFishV2.png
  3. Save the changes and run the application once again.
The application is deployed, but
http://localhost:8080/JAXWSWebApp/MathService
displays a generic Hello, World! message, and {http://localhost:8080/JAXWSWebApp/MathService?wsdl} gives a 404 error.

Amending the web.xml descriptor


As I wrote before, Maven archetypes are pretty out of date. Open
/WEB-INF/web.xml
and replace the old web 2.3 descriptor:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>

with a 2.5 compliant one:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
</web-app>

Running the project


Redeploy and run the application. You see an information page about the service at
http://localhost:8080/JAXWSWebApp/MathService
.

http://wiki.netbeans.org/attach/JAXWSNB6Maven2GlassFishV2/ws-info_JAXWSNB6Maven2GlassFishV2.png

The link to the WSDL on this page does not work, but you can view the WSDL by appending
?wsdl
to the URL of the information page ({http://localhost:8080/JAXWSWebApp/MathService?wsdl}). If you use GlassFish V2, you can test all deployed Web Services by appending
Tester
as a query string, just like this:
http://localhost:8080/JAXWSWebApp/MathService?Tester

The above URL generates a simple web form which can be used for quick testing:

http://wiki.netbeans.org/attach/JAXWSNB6Maven2GlassFishV2/glashfish_web_service_tester_JAXWSNB6Maven2GlassFishV2.png

Summary


Congratulations you have just written your very first JAX-WS Web Service using NetBeans 6, Maven2, and GlassFish V2.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo