Client開發(fā)者擁有Web服務(wù)端的class 即:能拿到服務(wù)端的接口Class和Entity類及aegis 配置文件 //傳統(tǒng)POJO模式 Service srvcModel = new ObjectServiceFactory().create(Hello.class); XFireProxyFactory factory = new XFireProxyFactory( XFireFactory.newInstance().getXFire()); String helloWorldURL = " http://localhost:8080/webservice/services/Hello "; Hello srvc = ( Hello) factory.create(srvcModel,helloWorldURL);//Hello為接口 System.out.println(srvc.sayHello("Robin")); // JSR181模式,注意這里Server端開發(fā)組需要向Client提供實(shí)現(xiàn)類,而不止于//接口,有點(diǎn)危險(xiǎn) Service srvcModel = new AnnotationServiceFactory() .create(HelloImpl.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory .newInstance().getXFire()); String helloWorldURL = "http://localhost:8080/webservice/services/Hello"; Hello hello = (Hello) factory.create(srvcModel,helloWorldURL); System.out.println(hello.sayHello("caohuili:")); 、2 動(dòng)態(tài)模式 動(dòng)態(tài)模式不需要服務(wù)端的class,不過性能和復(fù)雜對(duì)象映射等估計(jì)做得不會(huì)太好。 Client client = new Client(new URL( "http://localhost:8080/webservice/services/Hello?wsdl")); Object[] results = client .invoke("sayHello", new Object[] { "Cao," }); System.out.println((String) results[0]); 3 根據(jù)WSDL生成Client Stub //簡(jiǎn)單參數(shù)的 HelloClient client=new HelloClient(); HelloPortType port =client.getHelloHttpPort(); System.out.println(port.sayHello("hi!")); //參數(shù)復(fù)雜的 MemberServiceClient client = new MemberServiceClient(); MemberService memberService = client.getMemberServiceHttpPort(); User user = new User(); /*JAXBElement<String> name = new JAXBElement<String>(new QName( "http://memberService", "username"), String.class, "smallnest"); JAXBElement<String> password = new JAXBElement<String>(new QName( "http://memberService", "password"), String.class, "123456");*/ ObjectFactory objFac=new ObjectFactory(); JAXBElement<String> name = objFac.createUserUsername("smallnest"); JAXBElement<String> password = objFac.createUserPassword("123456"); user.setUsername(name); user.setPassword(password); Member oneMember = new Member(); JAXBElement<String> memeberName = new JAXBElement<String>(new QName( "http://memberService", "name"), String.class, "邁克"); JAXBElement<String> email = new JAXBElement<String>(new QName( "http://memberService", "email"), String.class, "micro@micro.com"); oneMember.setName(memeberName); oneMember.setEmail(email); oneMember.setRank(6); // 調(diào)用方法 memberService.addMember(oneMember, user); |
|