Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Remove "Example" texts from examples (makes more Docbook-friendly)

...

For example, an implementation class for a service that defined the operations sayHi and greetMe may look like #Example1the following:

Anchor
Example1
Example1

Code Block
titleExample 1: Implementation of the Greeter Service
package demo.hw.server;

import org.apache.hello_world_soap_http.Greeter;

@javax.jws.WebService(portName = "SoapPort", serviceName = "SOAPService",
 targetNamespace = "http://apache.org/hello_world_soap_http",
 endpointInterface = "org.apache.hello_world_soap_http.Greeter")

public class GreeterImpl implements Greeter {

 public String greetMe(String me)
{
        System.out.println("Executing operation greetMe");
        System.out.println("Message received: " + me + "\n");
        return "Hello " + me;
     }

 public String sayHi()
{
        System.out.println("Executing operation sayHi\n");
        return "Bonjour";
     }
}

...

Tip
titleTip

JAX-WS defines an annotation that allows you to specify methods that are not exposed as part of a service. However, the best practice is to leave such methods out of the SEI.

#Example2 The below shows a simple SEI for a stock updating service.

Anchor
Example2
Example2

Code Block
titleExample2:Simple SEI
package org.apache.cxf;

public interface QuoteReporter
{
  public Quote getQuote(String ticker);
}

...

Because the SEI is a standard Java interface, the class that implements it is just a standard Java class. If you started with a Java class you will need to modify it to implement the interface. If you are starting fresh, the implementation class will need to implement the SEI.

#Example3 The below shows a class for implementing the above interface in #Example2.

Anchor
Example3
Example3

Code Block
titleExample3:Implementation for SEI
package org.apache.cxf;

import java.util.*;

public class StockQuoteReporter implements QuoteReporter
{
  ...
public Quote getQuote(String ticker)
  {
    Quote retVal = new Quote();
    retVal.setID(ticker);
    retVal.setVal(Board.check(ticker));[1]
    Date retDate = new Date();
    retVal.setTime(retDate.toString());
    return(retVal);
  }
}

...

The SEI requires that you add the @WebService annotation. Since the SEI is the contract that defines the service, you should specify as much detail as you can about the service in the @WebService annotation's properties.

#Example4 The code below shows the interface defined in #Example2 above with the @WebService annotation.

Anchor
Example4
Example4

Code Block
titleExample4:Interface with the @WebService Annotation
package com.mycompany.demo;

import javax.jws.*;

@WebService(name="quoteUpdater",
            targetNamespace="http://cxf.apache.org",
            wsdlLocation="http://somewhere.com/quoteExampleService?wsdl")
public interface QuoteReporter
{
  public Quote getQuote(@WebParam(name="ticker") String ticker);
}

The @WebService annotation in #Example4 above does the following:

  1. Specifies that the value of the name attribute of the wsdl:portType element defining the service interface is quoteUpdater.
  2. Specifies that the target namespace of the service is http://cxf.apache.org.
  3. Specifies that the service will use the pre-defined WSDL contract which is published at http://somewhere.com/quoteExampleService?wsdl.

...

Anchor
Example5
Example5

Code Block
titleExample5:Annotated Service Implementation Class
package org.apache.cxf;

import javax.jws.*;

@WebService(endpointInterface="org.apache.cxf.quoteReporter",
            targetNamespace="http://cxf.apache.org",
            portName="StockQuotePort",
            serviceName="StockQuoteReporter",
            )
public class StockQuoteReporter implements QuoteReporter
{
public Quote getQuote(String ticker)
  {
  ...
  }
}

...

Property

Values

Description

style

Style.DOCUMENT (default)
Style.RPC

Specifies the style of the SOAP message. If RPC style is specified, each message part within the SOAP body is a parameter or return value and will appear inside a wrapper element within the soap:body element. The message parts within the wrapper element correspond to operation parameters and must appear in the same order as the parameters in the operation. If DOCUMENT style is specified, the contents of the SOAP body must be a valid XML document, but its form is not as tightly constrained.

use

Use.LITERAL (default)
Use.ENCODED

Specifies how the data of the SOAP message is streamed.

parameterStyle

ParameterStyle.BARE
ParameterStyle.WRAPPED (default)

Specifies how the method parameters, which correspond to message parts in a WSDL contract, are placed into the SOAP message body. A parameter style of BARE means that each parameter is placed into the message body as a child element of the message root. A parameter style of WRAPPED means that all of the input parameters are wrapped into a single element on a request message and that all of the output parameters are wrapped into a single element in the response message. If you set the style to RPC you must use the WRAPPED parameter style.

#Example6 shows an An SEI that uses rpc/literal SOAP messages .is as follows:

Anchor
Example6
Example6

Code Block
titleExample6:Specifying an RPC/LITERAL SOAP Binding
package org.eric.demo;

import javax.jws.*;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.*;

@WebService(name="quoteReporter")
@SOAPBinding(style=Style.RPC, use=Use.LITERAL)
public interface QuoteReporter
{
  ...
}

...

The @Oneway annotation is defined by the javax.jws.Oneway interface. It is placed on the methods in the SEI that will not require a response from the service. The @Oneway annotation tells the run time that it can optimize the execution of the method by not waiting for a response and not reserving any resources to process a response.

Example

#Example7 The below shows an SEI whose methods are annotated.

Anchor
Example7
Example7

Code Block
titleExample7:SEI with Annotated Methods
package org.apache.cxf;

import javax.jws.*;
import javax.xml.ws.*;

@WebService(name="quoteReporter")
public interface QuoteReporter
{
  @WebMethod(operationName="getStockQuote")
  @RequestWrapper(targetNamespace="http://demo.mycompany.com/types",
                  className="java.lang.String")
  @ResponseWrapper(targetNamespace="http://demo.mycompany.com/types",
                   className="org.eric.demo.Quote")
  public Quote getQuote(String ticker);
}

...

Property

Description

name

Specifies the name of the return value as it appears in the WSDL. For RPC bindings, this is name of the wsdl:part representing the return value. For document bindings, this is the local name of the XML element representing the return value. The default value is return.

targetNamespace

Specifies the namespace for the return value. It is only used with document bindings where the return value maps to an XML element. The defaults is to use the service's namespace.

header

Specifies if the return value is passed as part of the SOAP header.

partName

Specifies the value of the name attribute of the wsdl:part element for the return value when the binding is document.

Example

#Example8 The next example shows an SEI that is fully annotated.

Anchor
Example8
Example8

Code Block
titleExample8:Fully Annotated SEI
package org.apache.cxf;

import javax.jws.*;
import javax.xml.ws.*;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.*;
import javax.jws.WebParam.*;

@WebService(name="quoteReporter")
@SOAPBinding(style=Style.RPC, use=Use.LITERAL)
public interface QuoteReporter
{
  @WebMethod(operationName="getStockQuote")
  @RequestWrapper(targetNamespace="http://demo.mycompany.com/types",
                  className="java.lang.String")
  @ResponseWrapper(targetNamespace="http://demo.mycompany.com/types",
                   className="org.eric.demo.Quote")
  @WebResult(targetNamespace="http://demo.mycompany.com/types",
             name="updatedQuote")
  public Quote getQuote(
                        @WebParam(targetNamespace="http://demo.mycompany.com/types",
                                  name="stockTicker",
                                  mode=Mode.IN)
                        String ticker
  );
}

...

Once you have annotated your code, you can generate a WSDL contract for your service using the java2wsdl command.

Example

#Example9 The next example shows the WSDL contract generated for the SEI shown in #Example8. above:

Anchor
Example9
Example9

Code Block
titleExample9:Generated WSDL from an SEI
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://demo.eric.org/"
                  xmlns:tns="http://demo.eric.org/"
		  xmlns:ns1=""
		  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
		  xmlns:ns2="http://demo.eric.org/types"
		  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
		  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <xsd:schema>
      <xs:complexType name="quote">
        <xs:sequence>
          <xs:element name="ID" type="xs:string" minOccurs="0"/>
          <xs:element name="time" type="xs:string" minOccurs="0"/>
          <xs:element name="val" type="xs:float"/>
        </xs:sequence>
      </xs:complexType>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="getStockQuote">
    <wsdl:part name="stockTicker" type="xsd:string">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="getStockQuoteResponse">
    <wsdl:part name="updatedQuote" type="tns:quote">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="quoteReporter">
    <wsdl:operation name="getStockQuote">
      <wsdl:input name="getQuote" message="tns:getStockQuote">
    </wsdl:input>
      <wsdl:output name="getQuoteResponse" message="tns:getStockQuoteResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="quoteReporterBinding" type="tns:quoteReporter">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getStockQuote">
      <soap:operation style="rpc"/>
      <wsdl:input name="getQuote">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="getQuoteResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="quoteReporterService">
    <wsdl:port name="quoteReporterPort" binding="tns:quoteReporterBinding">
      <soap:address location="http://localhost:9000/quoteReporterService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>