测试CXF支持的数据类型

1.基本类型
–int,float,boolean等
2.引用类型
–String
–集合:数组,List, Set, Map

–自定义类型   Student


服务端

javaBean

package com.me.ws.bean;

public class Person {
	private Integer id;
	private String name;
	private Integer age;

	public Person() {

	}

	public Person(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

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

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
    
}

SEI接口

package com.me.ws2;

import java.util.List;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;

import com.me.ws.bean.Person;

/*
 * SEI
 */
@WebService
public interface PersonWS {
	@WebMethod
	public boolean addPerson(Person person);
	
	@WebMethod
	public Person getPersonById(int id);
	@WebMethod
	public List<Person> getPersonList(int age);
	@WebMethod
	public Map<Integer,Person> getAllPersonsMap();
}


SEI接口实现类

package com.me.ws2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.me.ws.bean.Person;

/*
 * SEI的实现
 */
@WebService
public class PersonWSImpl implements PersonWS {

	@Override
	public boolean addPerson(Person person) {
		System.out.println("addPerson(Person person)"+person.toString());
		return true;
	}

	@Override
	public Person getPersonById(int id) {
		// TODO Auto-generated method stub
		return new Person(id, "小明", 20);
	}

	@Override
	public List<Person> getPersonList(int age) {
		List<Person> list = new ArrayList<>();
		list.add(new Person(1, "tom", 20));
		list.add(new Person(2, "tom2", 20));
		list.add(new Person(3, "tom3", 20));
		return list;
	}

	@Override
	public Map<Integer, Person> getAllPersonsMap() {
		Map<Integer, Person> map = new HashMap<>();
		map.put(1, new Person(1, "jack1", 12));
		map.put(2, new Person(2, "jack2", 12));
		map.put(3, new Person(3, "jack3", 12));
		return map;
	}



}

发布服务

package com.me.ws.server;

import javax.xml.ws.Endpoint;

import com.me.ws2.PersonWSImpl;

/*
 * 发布Web Service
 */
public class ServerTest {
	public static void main(String[] args) {
	
		
		String address = "http://192.168.1.104:8787/cxf/personWS";
		Endpoint.publish(address, new PersonWSImpl());
		System.out.println("发布webservice成功!");
	}
}

使用cxf工具包生成客户端代码


客户端测试代码

package com.me.ws.test;

import java.util.List;

import com.me.ws2.GetAllPersonsMapResponse.Return;
import com.me.ws2.GetAllPersonsMapResponse.Return.Entry;
import com.me.ws2.Person;
import com.me.ws2.PersonWS;
import com.me.ws2.PersonWSImplService;

public class ClientTest2 {
   public static void main(String[] args) {
	 PersonWSImplService factory = new PersonWSImplService();
	 PersonWS personWS = factory.getPersonWSImplPort();
	 
	 boolean flag = personWS.addPerson(new Person(10,1,"jack"));
	 System.out.println(flag);
	 
	 Person person = personWS.getPersonById(1);
	 System.out.println(person);
	 
	 List<Person> personList = personWS.getPersonList(2);
	 System.out.println(personList);
	 
	 Return result = personWS.getAllPersonsMap();
	 List<Entry> entrys = result.getEntry();
	 for (Entry entry : entrys) {
		System.out.println("id="+entry.getKey()+entry.getValue());
		
	}
	 
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值