如何抛出一个自定义的错误在JAX-WS Web服务?(How to throw a custom f

2019-07-04 04:33发布

你如何抛出一个自定义的SOAP错误在JAX-WS Web服务? 我如何指定faultCodefaultStringdetail SOAP错误的? 是否有可能设置的值, detail豆,而不是一个String

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

Answer 1:

使用@WebFault注解。

你可以看到一个很好的例子在Java中JAX-WS Web服务使用SOAP故障和异常-埃本休伊特上的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;
    }
}

UPDATE

另一种方法是申报典型的例外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>


Answer 2:

import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.soap.SOAPFaultException;
import javax.xml.namespace.QName;
...

SOAPFactory soapFactory = SOAPFactory.newInstance();
SOAPFault soapFault = soapFactory.createFault(
        "Your custom message", 
         new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client")); 
throw new SOAPFaultException(soapFault);

要选择合适的故障代码,见http://www.tutorialspoint.com/soap/soap_fault.htm 。



文章来源: How to throw a custom fault on a JAX-WS web service?