KAD KAD - 1 year ago 108
Java Question

Updating SOAP 1.2 fault "code value" and "locale"?

I have set a custom WebFault exception as follows:

@WebFault(name="WSFault", targetNamespace = "http://www.example.com/")
public class WSException extends Exception {

private static final long serialVersionUID = -6647544772732631047L;
private WSFault fault;

and I am getting the below fault XML when throwing my custom exception at the level of the endpoint :
throw new WSException("1234","My Service Error");

<S:Fault xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/">
<S:Text xml:lang="en">My Service Error</S:Text>
<ns2:WSFault xmlns:ns2="http://www.example.com/">
<faultString>My Service Error</faultString>

I want to control the
value in the
tag to allow specifiying the language of the error message sent as well as the code value
. Is there a way to do this with

Answer Source

I was able to resolve this using the JAX-WS API objects and creating a custom fault message rather than just throwing an exception. That way you will be able to access and build all the tags the way you want:

// Create a new SOAP 1.2 message from the message factory and obtain the SOAP body
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPBody soapBody = message.getSOAPPart().getEnvelope().getBody();

// get the fault
SOAPFault fault =  soapBody.addFault();

// since this is an error generated from the business application
// where SOAPValue is the standard value code "Sender|Reciever...etc"
QName faultName = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, SOAPValue);

// set the fault reason text
// where languageLocale is the passed language local, the Locale object can be used
fault.addFaultReasonText(errorMessage, languageLocale);

// throw the exception that shall generate the SOAP fault response XML message
throw new SOAPFaultException(fault);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download