栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

使用

@WebFault
注释。

您可以在Java JAX-WS
Web服务
中的使用SOAP错误和异常-Java上的Eben
Hewitt中看到一个很好的示例。

您将看到示例:

@WebFault(name="CheckVerifyFault",    targetNamespace="http://www.example.com")public class CheckVerifyFault extends Exception {        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">         <faultpre>S:Server</faultpre>         <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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438124.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号