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

Arc*_*rci 32 java web-services exception jax-ws soapfault

如何在JAX-WS Web服务上抛出自定义soap错误?我怎么可以指定faultCode,faultStringdetailSOAP错误的?是否可以设置detailas bean 的值而不是String

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

Pau*_*gas 31

使用@WebFault注释.

您可以在Java JAX-WS Web服务使用SOAP故障和异常--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;
    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE

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

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

package pkg.ex;

public class FooException extends Exception {

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

}
Run Code Online (Sandbox Code Playgroud)

下一课是服务实现.

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

}
Run Code Online (Sandbox Code Playgroud)

如果我的要求是:

<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>
Run Code Online (Sandbox Code Playgroud)

没有问题:

<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>
Run Code Online (Sandbox Code Playgroud)

但...

<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>
Run Code Online (Sandbox Code Playgroud)

然后...

<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>
Run Code Online (Sandbox Code Playgroud)

  • 嗨!谢谢你的链接!搞定了.有没有办法让我也可以指定我的faultCode?如何使用上面的代码返回自定义faultCode? (2认同)
  • @Arci,我有同样的问题,除非你自己构造一个SoapFaultException,否则是不可能的.真正的垃圾! (2认同)

Jul*_*ien 7

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);
Run Code Online (Sandbox Code Playgroud)

要选择正确的故障代码,请参见http://www.tutorialspoint.com/soap/soap_fault.htm