java - 如何在 JAX-WS Web 服务上引发自定义错误?

标签 java web-services exception jax-ws soapfault

如何在 JAX-WS Web 服务上引发自定义的 soap 错误?如何指定soap故障的faultCodefaultStringdetail?是否可以将 detail 的值设置为 bean 而不是 String

请注意,我正在使用代码优先方法进行开发。

最佳答案

使用 @WebFault 注释。

您可以在 Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java 中看到一个很好的例子.

你会看到这个例子:

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

    /**
     * Java type that goes as soapenv:Fault detail element.
     */
    private CheckFaultBean faultInfo;

    public CheckVerifyFault(String message, CheckFaultBean faultInfo) {
        super(message);
        this.faultInfo = faultInfo;
    }

    public CheckVerifyFault(String message, CheckFaultBean faultInfo, 
           Throwable cause) {
        super(message, cause);
        this.faultInfo = faultInfo;
    }

    public CheckFaultBean getFaultInfo() {
        return faultInfo;
    }
}

更新

另一种方法是在 throws 子句中声明典型异常

例如假设以下是我的异常类:

package pkg.ex;

public class FooException extends Exception {

    public FooException(String message, Throwable cause) {
        super(message, cause);
    }

}

下一个类是服务实现。

package pkg.ws;

import javax.jws.WebService;
import pkg.ex.FooException;

@WebService(serviceName = "FooSvc")
public class FooService {

    public String sayHello(String name) throws FooException {
        if (name.isEmpty()) {
            Throwable t = new IllegalArgumentException("Empty name");
            throw new FooException("There is one error", t);
        }
        return "Hello, " + name;
    }

}

如果我的要求是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:ws="http://ws.pkg/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:sayHello>
         <arg0>Peter</arg0>
      </ws:sayHello>
   </soapenv:Body>
</soapenv:Envelope>

没有问题:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:sayHelloResponse xmlns:ns2="http://ws.pkg/">
         <return>Hello, Peter</return>
      </ns2:sayHelloResponse>
   </S:Body>
</S:Envelope>

但是……

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:ws="http://ws.pkg/">
   <soapenv:Header/>
   <soapenv:Body>
      <ws:sayHello>
         <arg0></arg0>
      </ws:sayHello>
   </soapenv:Body>
</soapenv:Envelope>

那么……

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
         <faultcode>S:Server</faultcode>
         <faultstring>There is one error</faultstring>
         <detail>
            <ns2:FooException xmlns:ns2="http://ws.pkg/">
               <message>There is one error</message>
            </ns2:FooException>
         </detail>
      </S:Fault>
   </S:Body>
</S:Envelope>

关于java - 如何在 JAX-WS Web 服务上引发自定义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13596260/

相关文章:

java - Jdbc DataSource多线程不释放连接

java - 如何在用户界面中使用 SOAP Web 服务?

c# - 尝试在 ASP.NET 网站中托管时出现 WCF 服务异常错误

android - 停止动画并重新开始

java - (数字/数字)的正则表达式

java - 使用 JarBundler 将 Java 转换为适用于 MacOSX 的 .app 文件

java - java中的几个时区没有设置偏移量

asp.net - Jquery Ajax 调用 Web 服务失败

SharePoint 自定义 Web 服务消耗问题 - HTTP 401 : Unauthorized

javascript - 为什么我的 if 条件不能阻止我的脚本崩溃?