如何使用 JAX WS 通过 SOAP 提供详细的错误信息?

发布于 2024-11-04 11:25:49 字数 1884 浏览 3 评论 0 原文

我正在使用 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-11-11 11:25:49

问题的答案是,可以在WebFault中提供详细的错误信息。

例如,

@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;
    }
}

Errors 类可以定义为,

@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;
    }

}

就像 message 变量一样,错误类可以具有任意数量的变量,这些变量可以保存值并且可以在客户端访问。

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.

萌逼全场 2024-11-11 11:25:49

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文