How to provide detailed error information over SOAP with JAX WS?
I'm developing web services using a Java-first approach with JAX-WS. I'm struggling to figure out how to provide detailed error information to the web service client. Ideally, I would like to throw an instance of the class below when there are validation errors:
public class ValidationException extends Exception {
private Errors errors;
public ValidationException(Errors errors) {
this.errors = errors;
}
public Errors getErrors() {
this.errors;
}
}
The Errors
object (similar to Spring's Errors interface) encapsulates:
- the fields that are in error
- the nature of each error (unique constraint violated, allowed range exceeded, etc.)
ValidationException
is thrown by the operations of my service endpoint that perform validation, e.g.
public class MyEndpoint {
public void doSomething(ValidateableInput input) throws ValidationException {
// implementation omitted
}
}
A client accesses the service using code generated by wsimport. However this code does not throw an instance of my ValidationException
class, but instead throws an instance of an exception class generated by wsimport
(this class is also named ValidationException
but is in a different package). The client's ValidationException
does not contain the Errors
object, so detailed information about the cause of the error is lost.
In the context of SOAP/JAX-WS the instance of ValidationException
thrown by the service has to be translated to a a SOAP fault. I have read this article about Faults in JAX-WS which demonstrates how to use a Fault Bean to encapsulate further detail about your exception. However, the Fault Bean seems to be limited to:
a Java class that has a no-arg constructor, a message String field, and a getter and setter for it. This will be your carrier for that soap:fault detail element.
so it only prov开发者_运维问答ides a single String in which to encapsulate information about the cause of the error(s).
To summarise: is it possible to provide detailed error information in the client code generated for a JAX-WS service
The answer to the question is yes, it is possible to provide detailed error information in WebFault.
E.g.
@WebFault(faultBean = "com.myexample.Errors")
public class ValidationException extends Exception {
private static final long serialVersionUID = 1L;
private Errors errors;
public ValidationException() {
super();
}
public ValidationException(String message, Errors errors, Throwable cause) {
super(message, cause);
this.errors = errors;
}
public ValidationException(String message, Errors errors) {
super(message);
this.errors = errors;
}
public Errors getErrors() {
return errors;
}
}
The Errors class can be defined as,
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Errors", propOrder = {
"message"
})
public class Errors {
@XmlElement(required = true)
protected String message;
public String getMessage() {
return message;
}
public void setMessage(String value) {
this.message = value;
}
}
Like message variable the error class can have any number of variables which can hold values and can be accessible at client end.
In a web service if you throw an exception, it will go to the client encapsulated in a SoapFault, but all your exception is inside it.
In your specific example, how is ValidationException declared in your WSDL? It has any field? I think your problem is the absence of a setErrors() method in your ValidationException calss. The JAXB creates the the description of your classes by default (if no JAXB annotation are used) adding as fields just what has a getter-setter pair.
精彩评论