我正在使用 Java 优先方法和 JAX-WS 来开发 Web 服务。我正在努力弄清楚如何向 Web 服务客户端提供详细的错误信息。理想情况下,当出现验证错误时,我想抛出下面的类的实例:
public class ValidationException extends Exception {
private Errors errors;
public ValidationException(Errors errors) {
this.errors = errors;
}
public Errors getErrors() {
this.errors;
}
}
Errors
对象(类似于 Spring 的 Errors 接口) 封装:
- 出错的字段
- 每个错误的性质(违反唯一约束、允许范围超出等)
ValidationException
由执行验证的服务端点的操作抛出,例如
public class MyEndpoint {
public void doSomething(ValidateableInput input) throws ValidationException {
// implementation omitted
}
}
客户端使用 wsimport。但是,此代码不会抛出我的 ValidationException
类的实例,而是抛出由 wsimport
生成的异常类的实例(此类也名为 ValidationException< /code> 但位于不同的包中)。客户端的 ValidationException
不包含 Errors
对象,因此有关错误原因的详细信息会丢失。
在 SOAP/JAX-WS 上下文中,服务抛出的 ValidationException
实例必须转换为 SOAP 错误。我已阅读这篇关于 JAX 错误的文章 -WS 其中 演示如何使用故障 Bean 来封装有关异常的更多详细信息。然而,Fault Bean 似乎仅限于:
一个 Java 类,它有一个无参数构造函数、一个消息字符串字段以及一个 getter 和 setter。这将是您的soap:fault 详细信息元素的载体。
因此它只提供一个字符串来封装有关错误原因的信息。
总结一下:是否可以在为 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 provides 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
发布评论
评论(2)
问题的答案是是,可以在WebFault中提供详细的错误信息。
例如,
Errors 类可以定义为,
就像 message 变量一样,错误类可以具有任意数量的变量,这些变量可以保存值并且可以在客户端访问。
The answer to the question is yes, it is possible to provide detailed error information in WebFault.
E.g.
The Errors class can be defined as,
Like message variable the error class can have any number of variables which can hold values and can be accessible at client end.
在 Web 服务中,如果抛出异常,它将发送到封装在 SoapFault 中的客户端,但所有异常都在其中。
在您的具体示例中,您的 WSDL 中如何声明 ValidationException?有什么领域吗?我认为您的问题是 ValidationException 类中缺少 setErrors() 方法。默认情况下,JAXB 创建类的描述(如果没有使用 JAXB 注释),将具有 getter-setter 对的内容添加为字段。
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.