微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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


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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐