JAXWS学习(四)- 返回复杂对象
发布时间:2020-12-17 00:17:53 所属栏目:安全 来源:网络整理
导读:这一次,主要是看一下返回复杂对象 1.返回ListString 服务: 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
这一次,主要是看一下返回复杂对象 1.返回List<String>服务: 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> 重新加载项目,访问http://localhost:8080/jaxws-demo/listService 然后,我们生成客户端测试一下: 进入到客户端项目根路径,输入命令 D:WorkSpacesWorkSpace_SSMjaxws-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); } } 2.返回List<Person>我们先添加一个实体类 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; } } 重新加载项目,访问地址:http://localhost:8080/jaxws-demo/listService 我们使用命令,重新生成一下代码 D:WorkSpacesWorkSpace_SSMjaxws-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()); } } } 这里的传递的对象,应该是需要序列化的, 具体还没有深入学习,有待研究。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |