当前位置: 代码迷 >> Web前端 >> web service axis2 参数替对象传递
  详细解决方案

web service axis2 参数替对象传递

热度:344   发布时间:2012-10-07 17:28:51.0
web service axis2 参数为对象传递

首先建立ServiceTest.java

Java代码
1.package sample.ws.service;?
2.?
3.import sample.ws.pojo.Person;?
4.?
5.public class ServiceTest {?
6./**?
7. * 很简单我们只是将发过来的person[]返回去?
8. * @param person?
9. * @return?
10. */?
11.??? public Person[] getPerson(Person[] person){?
12.?????????
13.//????? Person person1 = new Person();?
14.//????? person1.setAge("20");?
15.//????? person1.setName("张三");?
16.//???????
17.//????? Person person2 = new Person();?
18.//????? person2.setAge("30");?
19.//????? person2.setName("李四");?
20.//???????
21.//????? Person[] person3 = new Person[2];?
22.//????? person3[0] = person1;?
23.//????? person3[1] = person2;?
24.//????? System.out.println("afd");?
25.??????? return person;?
26.??? }?
27.?????????
28.?????????
29.}?
package sample.ws.service;

import sample.ws.pojo.Person;

public class ServiceTest {
/**
* 很简单我们只是将发过来的person[]返回去
* @param person
* @return
*/
??? public Person[] getPerson(Person[] person){
???????
//??????? Person person1 = new Person();
//??????? person1.setAge("20");
//??????? person1.setName("张三");
//???????
//??????? Person person2 = new Person();
//??????? person2.setAge("30");
//??????? person2.setName("李四");
//???????
//??????? Person[] person3 = new Person[2];
//??????? person3[0] = person1;
//??????? person3[1] = person2;
//??????? System.out.println("afd");
??????? return person;
??? }
??????
??????
}

pojo Person.java 不变

?


Java代码
1.public class Person{?
2.?
3.??? private String name ;?
4.?
5.??? public String getName() {?
6.??????? return name;?
7.??? }?
8.?
9.??? public void setName(String name) {?
10.??????? this.name = name;?
11.??? }?
12.?????
13.}?
public class Person{

??? private String name ;

??? public String getName() {
??????? return name;
??? }

??? public void setName(String name) {
??????? this.name = name;
??? }
???
}

services.xml 也保持不变;


Java代码
1.<?xml version="1.0" encoding="UTF-8"?>?
2.?
3.?
4.<service name="TestService" scope="application">?
5.??????? <description>TestService</description>?
6.?
7.??? <messageReceivers>?????
8.??? <messageReceiver?????
9.??????????? mep="http://www.w3.org/2004/08/wsdl/in-only"???
10.??? class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>?????
11.??????? <messageReceiver?????
12.??????????? mep="http://www.w3.org/2004/08/wsdl/in-out"???
13.??? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>?????
14.??? </messageReceivers>???
15.??? <parameter name="ServiceClass">?
16.??? sample.ws.service.ServiceTest?
17.??? </parameter>?
18.?????
19.</service>?
<?xml version="1.0" encoding="UTF-8"?>


<service name="TestService" scope="application">
??????? <description>TestService</description>

??? <messageReceivers>?
??? <messageReceiver???
??????????? mep="http://www.w3.org/2004/08/wsdl/in-only"?
??? class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>?
??????? <messageReceiver?
??????????? mep="http://www.w3.org/2004/08/wsdl/in-out"?
??? class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>?
??? </messageReceivers>?
??? <parameter name="ServiceClass">
??? sample.ws.service.ServiceTest
??? </parameter>
???
</service>

下面就是客户端了:

1、 pojo Person.java 保持不变


Java代码
1.public class Person{?
2.?
3.??? private String name ;?
4.?
5.??? public String getName() {?
6.??????? return name;?
7.??? }?
8.?
9.??? public void setName(String name) {?
10.??????? this.name = name;?
11.??? }?
12.?????
13.}?
public class Person{

??? private String name ;

??? public String getName() {
??????? return name;
??? }

??? public void setName(String name) {
??????? this.name = name;
??? }
???
}

2、请求客户端 Ws2pojoClient.java


Java代码
1.package sample.ws.client;?
2.?
3.import javax.xml.namespace.QName;?
4.?
5.import org.apache.axis2.addressing.EndpointReference;?
6.import org.apache.axis2.client.Options;?
7.import org.apache.axis2.rpc.client.RPCServiceClient;?
8.?
9.import sample.ws.pojo.Person;?
10.?
11.public class Ws2pojoClient {?
12.?
13.??? public static void main(String args[]) throws java.lang.Exception {?
14.?
15.??????? RPCServiceClient client = new RPCServiceClient();?
16.?
17.??????? Options option = client.getOptions();?
18.??????? // 指定客户端访问的webservice服务器端地址?
19.??????? EndpointReference erf = new EndpointReference(?
20.??????????????? "http://localhost:9999/Ws2Pojo/services/TestService");?
21.?
22.??????? option.setTo(erf);?
23.??????? // 指定命名空间,指定要调用的方法?
24.??????? QName name = new QName("http://service.ws.sample", "getPerson");?
25.??????? // 创建Person对象?
26.??????? Person person1 = new Person();?
27.??????? person1.setAge("20");?
28.??????? person1.setName("张三");?
29.?
30.??????? Person person2 = new Person();?
31.??????? person2.setAge("30");?
32.??????? person2.setName("李四");?
33.??????? // 创建Person数组?
34.??????? Person[] person3 = new Person[2];?
35.??????? person3[0] = person1;?
36.??????? person3[1] = person2;?
37.?
38.??????? // 创建要传送的object数组?
39.??????? Object[] object = new Object[] { person3 };?
40.??????? // 创建返回的参数类型?
41.??????? Class[] returnTypes = new Class[] { Person[].class };?
42.??????? // 调用远程服务,得到返回的object数组?
43.??????? Object[] response = client.invokeBlocking(name, object, returnTypes);?
44.??????? // 强制转换成Person[]对象?
45.?
46.??????? Person[] p = (Person[]) response[0];?
47.??????? // 遍历得到我们刚刚请求过去的值?
48.??????? for (int i = 0; i < p.length; i++) {?
49.??????????? System.out.println(p.getAge());?
50.??????????? System.out.println(p.getName());?
51.??????? }?
52.?
53.??? }?
54.}?

深圳人才网?深圳招聘网?深圳人才招聘网?深圳人才大市场?

企业、个人免费注册,获取想要的?深圳?软件工程师招聘信息 月薪最低3000-8000,更有高端猎头职位!?

www.szrcwz.com???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?? ? ? ? ? ? ? ? ?Google 提供的广告

  相关解决方案