?
1、webService 手写客户端代码
? ? ?相比平台自动生成的客户端代码简单很多,只需要绑定wsdl的URL地址
?
//绑定URL,TestWebService:项目名称,Test:services.xml中标签<name>的值 String serviceURL="http://localhost:8080/TestWebService/services/Test"; //ITest.class是服务端的接口类 Service serviceModel=new ObjectServiceFactory().create(ITest.class,null,serviceURL,null); //创建工厂实例 XFireProxyFactory serviceFactory=new XFireProxyFactory(); //ITest是服务端的接口类 ITest test=(ITest)serviceFactory.create(serviceModel, serviceURL); //调用服务端方法example() test.example("Hello world");?
2、身份验证
服务端可以新建一个类来写验证,也可写在接口ITest的实现类TestImpl中;客户端写相应的验证方法,但均要继承AbstractHandler类而且需要配置services.xml
服务端:
?
public class TestImpl extends AbstractHandler implements ITest { /** * 身份验证 */ public void invoke(MessageContext ctx) throws Exception { // TODO Auto-generated method stub Element header = ctx.getInMessage().getHeader(); if (header == null) { throw new XFireRuntimeException("Missing SOAP Header"); } Element token = ctx.getInMessage().getHeader().getChild("AuthenticationToken"); if (token == null) { throw new XFireFault("----------请求必须包含身份验证信息!----------", XFireFault.SENDER); } // 获取客户端传递的参数Username和Password String username = token.getChild("Username").getValue(); String password = token.getChild("Password").getValue(); //验证 if(username.equals("root") && password.equals("123")){ System.out.println("验证通过"); } else{ System.out.println("验证未通过"); throw new XFireFault("非法的用户名和密码", XFireFault.SENDER); } } }?
客户端:
?
public class TestAbstractHandler extends AbstractHandler{ private String username; private String password; public TestAbstractHandler(String username,String password){ this.username=username; this.password=password; } public void invoke(MessageContext context) throws Exception { // TODO Auto-generated method stub //为SOAP Header构造验证信息 Element el = new Element("header"); context.getOutMessage().setHeader(el); Element auth = new Element("AuthenticationToken"); //传递Username Element username_el = new Element("Username"); username_el.addContent(username); //传递Password Element password_el = new Element("Password"); password_el.addContent(password); auth.addContent(username_el); auth.addContent(password_el); el.addContent(auth); } getters..setters.... }?
services.xml配置:
?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>Test</name> <!-- 服务端接口类--> <serviceClass>cn.rhui.webservice.ITest</serviceClass> <!-- 实现类--> <implementationClass> cn.rhui.webservice.TestImpl </implementationClass> <!-- 验证配置--> <inHandlers> <!-- 验证方法所在类--> <handler handlerClass="cn.rhui.webservice.TestImpl"></handler> </inHandlers> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service></beans>?
3、以xml格式传递参数并身份验证
参数:
<?xml version="1.0" encoding="utf-8"?>
<Login>
<Name>admin</Name>
<Password>123</Password>
</Login>
服务端:
?
/** * 接收xml格式参数 */ public boolean strParseXML(String str){ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder; String name=null; String password=null; try{ builder=factory.newDocumentBuilder(); //序列化参数 Document doc=builder.parse(new ByteArrayInputStream(str.getBytes())); //获取用户名 if(doc.getElementsByTagName("Name").item(0).getFirstChild()!=null){ name=doc.getElementsByTagName("Name").item(0).getFirstChild().getNodeValue(); } //获取密码 if(doc.getElementsByTagName("Password").item(0).getFirstChild()!=null){ password=doc.getElementsByTagName("Password").item(0).getFirstChild().getNodeValue(); } if(name.equals("admin")&&password.equals("123")){ System.out.println("欢迎"+name+",密码为:"+password); return true; } } catch(Exception e) { System.out.println("非法用户名和密码!"); e.printStackTrace(); } return false; }?
客户端main方法:
?
String xstr="<?xml version='1.0' encoding='utf-8'?>\n<Login>\n<Name>admin</Name>\n<Password>123</Password>\n</Login>"; boolean flag=test.strParseXML(xstr); if(flag){ System.out.println("xml传参且为合法用户"); }?
4、传递object对象参数
? 服务端建一个对象类:
?
public class userinfo { private String userName; private String sex; private int age; getter...setter... }?
客户端main方法:
?
//传递对象 userinfo user=new userinfo(); user.setUserName("张三"); user.setSex("男"); user.setAge(20); System.out.println("用户信息:"+test.example(user).getUserName()+"...");?
5、远程访问服务端
只需知道服务端访问地址:http://IP:8080/TestWebService/services/Test?wsdl写相应的客户端代码即可
6、在myEclipse中有一个webService专属的浏览器Web Services Explorer,可看到soap通信的源文件及详细信息