博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webservice系统学习笔记7-异常处理
阅读量:6920 次
发布时间:2019-06-27

本文共 7993 字,大约阅读时间需要 26 分钟。

接口类:IMyService.java

@WebResult(name="testExceptionResult")public void testException() throws MyException;

实现类:MyServiceImpl.java

@Overridepublic void testException() throws MyException{     throw new MyException("this is my exception");}

异常类:MyException.java

public class MyException extends Exception{    public MyException() {        super();        // TODO Auto-generated constructor stub    }    public MyException(String message, Throwable cause) {        super(message, cause);        // TODO Auto-generated constructor stub    }    public MyException(String message) {        super(message);        // TODO Auto-generated constructor stub    }    public MyException(Throwable cause) {        super(cause);        // TODO Auto-generated constructor stub    }}

 

生成的wsdl文件:

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

生成的xsd文件:

-
-
-
-
-
-

 使用myeclipse的Web Service Explorer调用该接口方法后返回的SOAP消息

S:Server
this is my exception
this is my exception
this is my exception

测试类:

@Test    public void test1() throws Exception{         String namespace = "http://ws01.yzl.com/";         String wsdlUrl = "http://localhost:8080/ws?wsdl";            //1、创建服务(Service)         URL url = new URL(wsdlUrl);         QName qname = new QName(namespace,"MyServiceImplService");         Service service = Service.create(url, qname);                  //2、创建Dispatch         //public interface Dispatch
extends BindingProviderDispatch 接口提供对动态调用服务端点操作的支持。javax.xml.ws.Service 接口作为创建 Dispatch 实例的工厂。 Dispatch
dispatch = service.createDispatch(new QName(namespace,"MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE); //3、创建SOAPMessage MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); SOAPBody body = envelope.getBody(); QName portQname = new QName(namespace, "testException","ns"); body.addBodyElement(portQname); //4、通过Dispatch传递消息,并返回响应消息 SOAPMessage returnMessage = dispatch.invoke(message); returnMessage.writeTo(System.out);//打印返回消息 System.out.println(); }

 

 结果:

javax.xml.ws.soap.SOAPFaultException: test exception    at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)    at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)    at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:176)    at com.sun.xml.internal.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:195)    at com.ws02.TestWsException.test1(TestWsException.java:42)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: com.ws01.MyException: test exception    at com.ws01.IServiceImp.testException(IServiceImp.java:10)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at com.sun.xml.internal.ws.api.server.InstanceResolver$1.invoke(InstanceResolver.java:235)    at com.sun.xml.internal.ws.server.InvokerTube$2.invoke(InvokerTube.java:135)    at com.sun.xml.internal.ws.server.sei.EndpointMethodHandler.invoke(EndpointMethodHandler.java:246)    at com.sun.xml.internal.ws.server.sei.SEIInvokerTube.processRequest(SEIInvokerTube.java:82)    at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:587)    at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:546)    at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:531)    at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:428)    at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:232)    at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:460)    at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:233)    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)    at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:65)    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:68)    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:555)    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:65)    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:527)    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)    at java.lang.Thread.run(Thread.java:619)

客户端抛出了SOAPFaultException异常,服务端没抛出异常

如果修改服务端的代码

throw new MyException("test exception");修改为:throw new RuntimeException("service runtime Exception");

那么客户端和服务端都将抛出异常,因为服务端并未将runtimeException异常抛给客户端。

抛出的异常客户端可以通过捕获SOAPFaultException来获取服务端的错误 

转载地址:http://mzkjl.baihongyu.com/

你可能感兴趣的文章
学习笔记-小甲鱼Python3学习第十六讲:序列
查看>>
wuzhicms发送邮件
查看>>
安装docker-compose
查看>>
IOS 利用桥接整合通讯录读取全部联系人
查看>>
就五分钟
查看>>
gradle使用maven镜像仓库的方法
查看>>
4月19日
查看>>
MySQL双主高可用架构之MMM实战
查看>>
JfreeChar柱状图实例
查看>>
MQ测试JAVA客户端实现源码
查看>>
求水仙花数
查看>>
SOS-Cacti thold alert mail
查看>>
lvs+keepalived
查看>>
关于新版本的驰骋工作流引擎投递算法更新说明
查看>>
Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff )
查看>>
Can an OSSEC manager have more than 256 agents
查看>>
关于chroot的一些解释
查看>>
让nginx 支持path_info
查看>>
python中的浅拷贝和深拷贝
查看>>
从软件的价值体系开始向技术的反向分析
查看>>