JAXWS学习(四)- 返回复杂对象

这一次,主要是看一下返回复杂对象

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

JAXWS学习(四)- 返回复杂对象_第1张图片

JAXWS学习(四)- 返回复杂对象_第2张图片

然后,我们生成客户端测试一下:

进入到客户端项目根路径,输入命令

D:\WorkSpaces\WorkSpace_SSM\jaxws-client>wsimport -keep -d bin -s src http://localhost:8080/jaxws-demo/listService?wsdl

JAXWS学习(四)- 返回复杂对象_第3张图片

测试代码:

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

JAXWS学习(四)- 返回复杂对象_第4张图片

我们使用命令,重新生成一下代码

D:\WorkSpaces\WorkSpace_SSM\jaxws-client>wsimport -keep -d bin -s src http://localhost:8080/jaxws-demo/listService?wsdl
JAXWS学习(四)- 返回复杂对象_第5张图片

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());
		}
	}
}

JAXWS学习(四)- 返回复杂对象_第6张图片

这里的传递的对象,应该是需要序列化的, 具体还没有深入学习,有待研究。


你可能感兴趣的:(list,webservice,服务,jaxws)