O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Addressing Faults

Problem

You want your declared faults (exceptions), and not just your operation inputs and outputs, to participate in Addressing.

Solution

Use the WS-Addressing @Action annotation’s fault attribute. It accepts a @FaultAction annotation for each exception your method throws.

Discussion

Most of the work is handled for you in the annotations. All you have to do is declare the fault action above the method that throws the exception you want to map with WS-Addressing. It accepts a Class instance indicating the exception you’re mapping, along with the URI you want to give as the address of the exception. The complete solution is shown in Examples 12-2 and 12-3.

Example 12-2. The web service that uses addressing on faults

package com.soacookbook;

import javax.jws.*;
import javax.xml.ws.Action;
import javax.xml.ws.FaultAction;
import javax.xml.ws.soap.Addressing;

/**
 * Shows a class that uses WS-Addressing, including
 * a Fault that is Addressing enabled.
 */
@WebService
@Addressing(required=true)
public class HelloAddressingFaultsWS {

  @WebMethod
  @Action(
    input="http://soacookbook.com/name",
    output="http://soacookbook.com/greeting", 
    fault={ 
      @FaultAction(className=HelloException.class, 
        value="http://soacookbook.com/myFault")})
  public String sayHello(String name) throws HelloException {
    
    if (name == null || "".equals(name)) {
      throw new HelloException("The name was null or empty.");
    }
    return "Hello, " + name;
  }
}

That’s all there is to it. Because the fault attribute of the Action ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required