webservice如何传递复杂参数和⾃定义参数
⾸先说参数为User对象。
服务器端照常如旧。还是⽤最初的⽂档中⽅式搭建。其中有个⽅法如下:
[html]
1. public User getUserName(User u){
2. User user= new User();
3. user.Age()+10);
4. user.setName( "^"+u.getName()+ "^");
5. user.Id()+1);
6. return user;
7. }
public User getUserName(User u){
User user= new User();
user.Age()+10);
user.setName("^"+u.getName()+ "^");
user.Id()+1);
return user;
}
那我们客户端如何传递过去呢?
当然我们还是使⽤Axiom⽅式。
因为在Axiom中,传递的参数必须为OMElement类型。所以呢。我们必须把我们⾃定义的对象转化成OMElement对象。
那如何转化呢?
[html]
1. //把user对象转化成相应的omElment对象
2. public static OMElement castElment(User u){
3. l.stream.XMLStreamReader reader = PullParser(u);
4. StreamWrapper parser = new StreamWrapper(reader);
5.
StAXOMBuilder stAXOMBuilder =ateStAXOMBuilder(OMAbstractFactory. getOMFactory(), parser)
6. OMElement element = DocumentElement();
7. return element;
8. }
//把user对象转化成相应的omElment对象
public static OMElement castElment(Useru){
StreamWrapper parser = newStreamWrapper(reader);
StAXOMBuilder ateStAXOMBuilder(OMAbstractFactory.
getOMFactory(),parser);
OMElement element =DocumentElement();
return element;
}
那我们我们继续分析转换后的OMElement结果?
OMElement param=castElment(u);
/
/输出转换后的omElement对象
System. out.println(param);
结果如下:
<User><age>10</age><id>0</id><name>李四</name></User>
实际上传递的是⼀串xml
main⽅式如下:
[html]
1. public static void main(String[] args) throws Exception {
2.
EndpointReference targetEPR = new EndpointReference("localhost:8080/testWSServerByAxis2/services/myService? wsdl" );
3. try {
4. Options options = new Options();
5. options.setTo(targetEPR);
6.
7. //加载⽬标服务器
8. ServiceClient sender = new ServiceClient();
9. sender.setOptions(options);
10.
11. OMFactory fac = OMFactory();
12. OMNamespace omNs = ateOMNamespace("test" , "" );
13. //调⽤服务端的⽅法
14. OMElement method = ateOMElement("getUserName" , omNs);
15. //给⽅法赋值参数值,参数值只能是omNode形式。所以需要把⾃定义对象转成omNode对象
16. //调⽤把⾃定义对象转化成omNode对象
17. User u= new User();
18. u.setId(0);
19. u.setAge(10);
调用webservice服务20. u.setName( "李四");
21. OMElement param= castElment(u);
22. //输出转换后的omElement对象
23. System.out.println(param);
24. method.addChild(param);
25. //返回OMElement对象
26. OMElement result = sender.sendReceive(method);
27. System.out.println("clientResult="+result);
28. Iterator ChildElements();
29. while(iterator.hasNext()){
30. System.out.println("ok");
31. OMNode omNode = (OMNode) ();
32. Type()==omNode. ELEMENT_NODE){
33. OMElement omElement = (OMElement) omNode;
34. System.out.println("***="+LocalName());
35. if (LocalName().toLowerCase().equals("return" )) {
36.
User u1 = (User) BeanUtil.processObject(omElement, User. class, null, true, new DefaultObjectSupplier());
37. System.out.Name());
38. }
39. }
40. }
41. } catch (Exception e) {
42. e.printStackTrace();
43. }
44. }
public static void main(String[] args) throwsException {
EndpointReference targetEPR =
newEndpointReference("localhost:8080/testWSServerByAxis2/services/myService?wsdl");
try {
Options options = newOptions();
options.setTo(targetEPR);
//加载⽬标服务器
ServiceClient sender = newServiceClient();
sender.setOptions(options);
OMFactory fac =OMFactory();
OMNamespace omNs =ateOMNamespace("test" , "" );
/
/调⽤服务端的⽅法
OMElement method =ateOMElement("getUserName" , omNs);
//给⽅法赋值参数值,参数值只能是omNode形式。所以需要把⾃定义对象转成omNode对象
//调⽤把⾃定义对象转化成omNode对象
User u= new User();
u.setId(0);
u.setAge(10);
u.setName( "李四");
OMElement param= castElment(u);
//输出转换后的omElement对象
System.out.println(param);
method.addChild(param);
//返回OMElement对象
OMElement result =sender.sendReceive(method);
System.out.println("clientResult="+result);
Iterator ChildElements();
while(iterator.hasNext()){
System.out.println("ok");
OMNode omNode = (();
Type()==omNode. ELEMENT_NODE){
OMElement omElement =(OMElement) omNode;
System.out.println("***="+LocalName());
LocalName().toLowerCase().equals("return" )) {
User u1 = (User)BeanUtil.processObject(omElement, User. class, null, true, newDefaultObjectSupplier());
System.out.Name());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
还是按照上篇博客的返回值,得到的结果如下:
[html]
1. clientResult=<ns:getUserNameResponse xmlns:ns="test">
<ns:return xmlns:ax21="entity/xsd"xmlns:xsi="/2001/XMLSchema-
instance"xsi:type="ax21:User"><ax21:age>20</ax21:age><ax21:id>1</ax21:id><ax21:name>^李四^</ax21:name>
</ns:return></ns:getUserNameResponse>
clientResult=<ns:getUserNameResponsexmlns:ns="test">
<ns:returnxmlns:ax21="entity/xsd"xmlns:xsi="/2001/XMLSchema-instance"
xsi:type="ax21:User"><ax21:age>20</ax21:age><ax21:id>1</ax21:id><ax21:name>^李四^</ax21:name></ns:return>
</ns:getUserNameResponse>
其次,若传递List<User>对象呢?
⼤家想想,List,如何传递呢?转化成消息⼜是在怎么样的呢?
因为传递参数必须为OmElement,那我们看⼀下List如何转化的OmElement对象呢?
[html]
1. //把List对象转化成相应的omElment对象
2. ublic static OMElement castElment(List<User> userList){
3. // 构建 OMFactory ⼯⼚
4. OMFactory factory = OMAbstractFactory. getOMFactory();
5. OMNamespace omNamespace = ateOMNamespace("test" , "" );
6.
OMElement omElement = BeanUtil. getOMElement(new QName("root"), Array(), new QName("user"), false, null);
7. return omElement;
//把List对象转化成相应的omElment对象
public static OMElementcastElment(List<User> userList){
// 构建 OMFactory ⼯⼚
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论