这里把Web服务端和客户端分开,用两个项目写
下面展示的是服务端代码
首先是web.xml和services.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>XFireExceptionDemo</name> <namespace>http://www.jadyer.com/XFireExceptionDemo</namespace> <serviceClass>com.jadyer.server.HelloExceptionService</serviceClass> <implementationClass>com.jadyer.server.HelloExceptionServiceImpl</implementationClass> </service> </beans>
接着是Web服务端暴露的接口及其实现类
package com.jadyer.server; import com.jadyer.server.exception.MyException; public interface HelloExceptionService { public String getException(String message) throws MyException; } package com.jadyer.server; import com.jadyer.server.exception.MyException; import com.jadyer.server.exception.MyExceptionDetail; public class HelloExceptionServiceImpl implements HelloExceptionService { public String getException(String sss) throws MyException { if("".equals(sss)){ throw new MyException("参数不能为空", new MyExceptionDetail("XFire异常测试成功")); }else{ return sss; } } }
然后是我们的自定义异常的描述类
package com.jadyer.server.exception; import java.io.Serializable; /** * 异常描述类 * @see 必须要显式或隐式的提供默认构造函数 */ public class MyExceptionDetail implements Serializable { private static final long serialVersionUID = -6043027006141857552L; private String message; public MyExceptionDetail(){} public MyExceptionDetail(String message){ this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
最后是我们的自定义异常的处理类
package com.jadyer.server.exception; import org.codehaus.xfire.fault.FaultInfoException; /** * 处理异常的基本类 * @see 这里主要继承了FaultInfoException类 */ public class MyException extends FaultInfoException{ private static final long serialVersionUID = -9010056196582741518L; private MyExceptionDetail med; public MyException(String message, MyExceptionDetail med){ super(message); this.med = med; } public MyExceptionDetail getFaultInfo(){ return med; } }
Web服务端代码,至此完毕
对于客户端,我们另建一个Java Project
package com.jadyer.client; import java.net.MalformedURLException; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.jadyer.server.HelloExceptionService; import com.jadyer.server.exception.MyException; /** * 客户端的对自定义异常的测试类 * @see 需要用到XFire 1.2 Core、HTTP Client Libraries两个类库 * @see 此时假设Web服务端已经提供了接口以及自定义异常的描述类和处理类 */ public class ExceptionClient { private static final String URL = "http://127.0.0.1:8080/XFire_Exception/services/XFireExceptionDemo"; private static final String namespace = "http://www.jadyer.com/XFireExceptionDemo"; public static void main(String[] args) { Service serviceModel = new ObjectServiceFactory().create(HelloExceptionService.class, null, namespace, null); try{ HelloExceptionService client = (HelloExceptionService)new XFireProxyFactory().create(serviceModel, URL); String result = client.getException("这是我进行异常测试时使用的字符串消息"); //此时不会出现异常 System.out.println(result); client.getException(""); //此时将会出现异常 }catch(MalformedURLException e){ e.printStackTrace(); }catch(MyException e){ e.printStackTrace(); System.out.println(e.getFaultInfo().getMessage()); } } }
然后运行ExceptionClient类,控制台若输出如下信息,则测试成功
这是我进行异常测试时使用的字符串消息 com.jadyer.server.exception.MyException: 参数不能为空 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at org.codehaus.xfire.client.ClientFaultConverter.processFaultDetail(ClientFaultConverter.java:57) at org.codehaus.xfire.client.ClientFaultConverter.invoke(ClientFaultConverter.java:32) at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131) at org.codehaus.xfire.client.Client.onReceive(Client.java:424) at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:139) at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48) at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26) at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131) at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79) at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114) at org.codehaus.xfire.client.Client.invoke(Client.java:336) at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77) at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57) at $Proxy0.getException(Unknown Source) at com.jadyer.client.ExceptionClient.main(ExceptionClient.java:27) XFire异常测试成功