1. Client开发者拥有Web服务端的class
即:能拿到服务端的接口Class和Entity类及aegis 配置文件
//传统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);
System.out.println(srvc.sayHello("Robin"));
// JSR181模式,注意这里Server端开发组需要向Client提供实现类,而不止于//接口,有点危险
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 动态模式
动态模式不需要服务端的class,不过性能和复杂对象映射等估计做得不会太好。
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 根据WSDL生成Client Stub
//简单参数的
HelloClient client=new HelloClient();
HelloPortType port =client.getHelloHttpPort();
System.out.println(port.sayHello("hi!"));
//参数复杂的
MemberServiceClient client = new MemberServiceClient();
MemberService memberService = client.getMemberServiceHttpPort();
User user = new User();
/*JAXBElement name = new JAXBElement (new QName(
"http://memberService", "username"), String.class, "smallnest");
JAXBElement password = new JAXBElement (new QName(
"http://memberService", "password"), String.class, "123456");*/
ObjectFactory objFac=new ObjectFactory();
JAXBElement name = objFac.createUserUsername("smallnest");
JAXBElement password = objFac.createUserPassword("123456");
user.setUsername(name);
user.setPassword(password);
Member oneMember = new Member();
JAXBElement memeberName = new JAXBElement (new QName(
"http://memberService", "name"), String.class, "迈克");
JAXBElement email = new JAXBElement (new QName(
"http://memberService", "email"), String.class,
"micro@micro.com");
oneMember.setName(memeberName);
oneMember.setEmail(email);
oneMember.setRank(6);
// 调用方法
memberService.addMember(oneMember, user);