这一次,主要是看一下返回复杂对象
服务:
package com.deppon.demo.service01; import java.util.ArrayList; import java.util.List; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(serviceName="ListService") public class ListService { @WebMethod() public List<String> getCitys() { List<String> citys = new ArrayList<String>(); citys.add("丹东"); citys.add("青岛"); citys.add("上海"); return citys; } }
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name="listService" implementation="com.deppon.demo.service01.ListService" url-pattern="/listService" > </endpoint> </endpoints>
然后,我们生成客户端测试一下:
进入到客户端项目根路径,输入命令
D:\WorkSpaces\WorkSpace_SSM\jaxws-client>wsimport -keep -d bin -s src http://localhost:8080/jaxws-demo/listService?wsdl
测试代码:
package com.deppon.demo.test; import java.util.List; import com.deppon.demo.service01.ListService; import com.deppon.demo.service01.ListService_Service; public class MainTest { public static void main(String[] args) { ListService service = new ListService_Service().getListServicePort(); List<String> citys = service.getCitys(); System.out.println("citys->" + citys); } }
我们先添加一个实体类
package com.deppon.demo.service01; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = -889590964471376392L; private Integer id; private String name; public Person(Integer _id , String _name) { this.id = _id; this.name = _name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.deppon.demo.service01; import java.util.ArrayList; import java.util.List; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(serviceName="ListService") public class ListService { @WebMethod() public List<String> getCitys() { List<String> citys = new ArrayList<String>(); citys.add("丹东"); citys.add("青岛"); citys.add("上海"); return citys; } public List<Person> getPersons() { List<Person> persons = new ArrayList<Person>(); persons.add(new Person(1 , "路飞")); persons.add(new Person(2 , "索隆")); persons.add(new Person(3 , "乔巴")); return persons; } }
我们使用命令,重新生成一下代码
D:\WorkSpaces\WorkSpace_SSM\jaxws-client>wsimport -keep -d bin -s src http://localhost:8080/jaxws-demo/listService?wsdl
package com.deppon.demo.test; import java.util.List; import com.deppon.demo.service01.ListService; import com.deppon.demo.service01.ListService_Service; import com.deppon.demo.service01.Person; public class MainTest { public static void main(String[] args) { ListService service = new ListService_Service().getListServicePort(); List<String> citys = service.getCitys(); System.out.println("citys->" + citys); List<Person> persons = service.getPersons(); for(Person each : persons) { System.out.println("each->" + each.getId() + "," + each.getName()); } } }
这里的传递的对象,应该是需要序列化的, 具体还没有深入学习,有待研究。