JBoss.orgCommunity Documentation

Chapter 2. Quick Start

2.1. Developing web service implementations
2.1.1. The service implementation class
2.1.2. Deploying service implementations
2.2. Consuming web services
2.2.1. Creating the client artifacts
2.2.2. Constructing a service stub
2.2.3. Resolving dependencies and running the client
2.3. Maven archetype quick start
2.4. Appendix
2.4.1. Sample wsdl contract

JBossWS uses WildFly as its target container. The following examples focus on web service deployments that leverage EJB3 service implementations and the JAX-WS programming models. For further information on POJO service implementations and advanced topics you need consult the user guide .

JAX-WS does leverage annotations in order to express web service meta data on Java components and to describe the mapping between Java data types and XML. When developing web service implementations you need to decide whether you are going to start with an abstract contract (WSDL) or a Java component.

If you are in charge to provide the service implementation, then you are probably going to start with the implementation and derive the abstract contract from it. You are probably not even getting in touch with the WSDL unless you hand it to 3rd party clients. For this reason we are going to look at a service implementation that leverages JSR-181 annotations .

Important

Even though detailed knowledge of web service meta data is not required, it will definitely help if you make yourself familiar with it. For further information see

When starting from Java you must provide the service implementation. A valid endpoint implementation class must meet the following requirements:

Let's look at a sample EJB3 component that is going to be exposed as a web service.

Don't be confused with the EJB3 annotation @Stateless . We concentrate on the @WebService annotation for now.

Service deployment basically depends on the implementation type. As you may already know web services can be implemented as EJB3 components or plain old Java objects. This quick start leverages EJB3 components, that's why we are going to look at this case in the next sections.

When creating web service clients you would usually start from the WSDL. JBossWS ships with a set of tools to generate the required JAX-WS artifacts to build client implementations. In the following section we will look at the most basic usage patterns. For a more detailed introduction to web service client please consult the user guide.

The wsconsume tool is used to consume the abstract contract (WSDL) and produce annotated Java classes (and optionally sources) that define it. We are going to start with the WSDL from our retail example (ProfileMgmtService.wsdl). For a detailed tool reference you need to consult the user guide.

Let's try it on our sample:

File

Purpose

ProfileMgmt.java

Service Endpoint Interface

Customer.java

Custom data type

Discount*.java

Custom data type

ObjectFactory.java

JAXB XML Registry

package-info.java

Holder for JAXB package annotations

ProfileMgmtService.java

Service factory

Basically wsconsume generates all custom data types (JAXB annotated classes), the service endpoint interface and a service factory class. We will look at how these artifacts can be used the build web service client implementations in the next section.

In order for successfully running a WS client application, a classloader needs to be properly setup to include the JBossWS components and its required transitive dependencies. Depending on the environment the client is meant to be run in, this might imply adding some jars to the classpath, or adding some artifact dependencies to the maven dependency tree, etc. Moreover, even for simply developing a client, users might need to resolve proper dependencies (e.g. to setup their IDE).

Below you find some options for resolving dependencies and running a WS client using the JBossWS libraries:

The JBossWS project is composed of multiple Maven artifacts that can be used to declare dependencies in user Maven projects. In particular, the org.jboss.ws.cxf:jbossws-cxf-client artifact can be used for getting the whole JBossWS client dependency. Users should simply add a dependency to it in their Maven project.

If you're running the client out of container, It's also recommended to properly setup JAXWS implementation endorsing, to make sure you use the JBossWS implementation of JAXWS API instead of relying on the implementation coming with the JDK; this is usually done by copying the org.jboss.ws.cxf.jbossws-cxf-factories (JBossWS-CXF stack) jar into a local directory (e.g. project.build.directory/endorsed ) and then using that for compiling and running sources, for setting the java.endorsed.dirs system property into the maven-surefire-plugin, etc:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 [http://maven.apache.org/maven-v4_0_0.xsd]">
  ...
  <build>
    <plugins>

      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>create-endorsed-dir</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>org.jboss.spec.javax.xml.ws</groupId>
                  <artifactId>jboss-jaxws-api_2.2_spec</artifactId>
                  <type>jar</type>
                  <outputDirectory>${project.build.directory}/endorsed</outputDirectory>
                </artifactItem>
                <artifactItem>
                  <groupId>org.jboss.spec.javax.xml.bind</groupId>
                  <artifactId>jboss-jaxb-api_2.2_spec</artifactId>
                  <type>jar</type>
                  <outputDirectory>${project.build.directory}/endorsed</outputDirectory>
                </artifactItem>
                <artifactItem>
                  <groupId>org.jboss.ws.cxf</groupId>
                  <artifactId>jbossws-cxf-factories</artifactId>
                  <type>jar</type>
                  <outputDirectory>${project.build.directory}/endorsed</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <showDeprecation>false</showDeprecation>
          <compilerArguments>
            <endorseddirs>${project.build.directory}/endorsed</endorseddirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>-Djava.endorsed.dirs=${project.build.directory}/endorsed</argLine>
          ...
        </configuration>
        ...
      </plugin>
      ...
    </plugins>
    ...
  </build>
...
</project>

Important

Endorsing of JAX-WS api jar is used to force a API level different from the one included in the JDK. E.g. JAXWS 2.2 on JDK 1.6, or JAXWS 2.1 on JDK 1.7, etc. So, depending on your environment, it might not be strictly required.

Note

Endorsing is deprecated in JDK 1.8 and will be removed in future JDK version. If you can't rely on endorsing with your JDK version, be sure JBossWS components (in particular org.jboss.ws.cxf:jbossws-cxf-factories ) come before jars of any other JAX-WS implementation in your classpath.

A convenient approach to start a new project aiming at providing and/or consuming a JAX-WS endpoint is to use the JBossWS jaxws-codefirst Maven Archetype. A starting project (including working build and sample helloworld client and endpoint) is created in few seconds. It's simply a matter of issuing a command and answering to simple questions on the desired artifact and group ids for the project being generated:

The generated project includes:

The project is built and tested by simply running:

The build processes the various plugins and calls into the JBossWS tools to generate all the required classes for building the deployment archive and client. The user can test the sample, have a look at the project structure and then either trash the sample endpoint and testcase and replace them with his own components, or modify them step-by-step to achieve what he needs.

<definitions
    name='ProfileMgmtService'
    targetNamespace='http://org.jboss.ws/samples/retail/profile'
    xmlns='http://schemas.xmlsoap.org/wsdl/'
    xmlns:ns1='http://org.jboss.ws/samples/retail'
    xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
    xmlns:tns='http://org.jboss.ws/samples/retail/profile'
    xmlns:xsd='http://www.w3.org/2001/XMLSchema'>

   <types>

      <xs:schema targetNamespace='http://org.jboss.ws/samples/retail'
                 version='1.0' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
         <xs:complexType name='customer'>
            <xs:sequence>
               <xs:element minOccurs='0' name='creditCardDetails' type='xs:string'/>
               <xs:element minOccurs='0' name='firstName' type='xs:string'/>
               <xs:element minOccurs='0' name='lastName' type='xs:string'/>
            </xs:sequence>
         </xs:complexType>
      </xs:schema>

      <xs:schema
          targetNamespace='http://org.jboss.ws/samples/retail/profile'
          version='1.0'
          xmlns:ns1='http://org.jboss.ws/samples/retail'
          xmlns:tns='http://org.jboss.ws/samples/retail/profile'
          xmlns:xs='http://www.w3.org/2001/XMLSchema'>

         <xs:import namespace='http://org.jboss.ws/samples/retail'/>
         <xs:element name='getCustomerDiscount'
                     nillable='true' type='tns:discountRequest'/>
         <xs:element name='getCustomerDiscountResponse'
                     nillable='true' type='tns:discountResponse'/>
         <xs:complexType name='discountRequest'>
            <xs:sequence>
               <xs:element minOccurs='0' name='customer' type='ns1:customer'/>

            </xs:sequence>
         </xs:complexType>
         <xs:complexType name='discountResponse'>
            <xs:sequence>
               <xs:element minOccurs='0' name='customer' type='ns1:customer'/>
               <xs:element name='discount' type='xs:double'/>
            </xs:sequence>
         </xs:complexType>
      </xs:schema>

   </types>

   <message name='ProfileMgmt_getCustomerDiscount'>
      <part element='tns:getCustomerDiscount' name='getCustomerDiscount'/>
   </message>
   <message name='ProfileMgmt_getCustomerDiscountResponse'>
      <part element='tns:getCustomerDiscountResponse'
            name='getCustomerDiscountResponse'/>
   </message>
   <portType name='ProfileMgmt'>
      <operation name='getCustomerDiscount'
                 parameterOrder='getCustomerDiscount'>

         <input message='tns:ProfileMgmt_getCustomerDiscount'/>
         <output message='tns:ProfileMgmt_getCustomerDiscountResponse'/>
      </operation>
   </portType>
   <binding name='ProfileMgmtBinding' type='tns:ProfileMgmt'>
      <soap:binding style='document'
                    transport='http://schemas.xmlsoap.org/soap/http'/>
      <operation name='getCustomerDiscount'>
         <soap:operation soapAction=''/>
         <input>

            <soap:body use='literal'/>
         </input>
         <output>
            <soap:body use='literal'/>
         </output>
      </operation>
   </binding>
   <service name='ProfileMgmtService'>
      <port binding='tns:ProfileMgmtBinding' name='ProfileMgmtPort'>

         <soap:address
             location='http://<HOST>:<PORT>/jaxws-samples-retail/ProfileMgmtBean'/>
      </port>
   </service>
</definitions>