WebService学习笔记-CXF支持的数据类型

此实例测试CXF支持的数据类型

Student.java实体类

public class Student {
	private int id;
	private String name;
	private float price;
	//...省略getter setter方法
	public Student() {//无参构造方法
		super();
	}
}

DataTypeWS.java接口

@WebService
public interface DataTypeWS {
	@WebMethod
	public boolean add(Student student);

	@WebMethod
	public Student getStudentById(int id);

	@WebMethod
	public List<Student> getStudentsByPrice(float price);

	@WebMethod
	public Map<Integer, Student> getAllStudentsMap();

}

DataTypeWSImpl.java实现接口

@WebService
public class DataTypeWSImpl implements DataTypeWS{

	@Override
	public boolean add(Student student) {
		System.out.println("Server add " + student);
		return false;
	}

	@Override
	public Student getStudentById(int id) {
		System.out.println("Server getStudentById " + id);
		return new Student(id, "umgsai", 15000);
	}

	@Override
	public List<Student> getStudentsByPrice(float price) {
		System.out.println("Server getStudentsByPrice " + price);
		List<Student>students = new ArrayList<Student>();
		students.add(new Student(1, "Tom", price + 1));
		students.add(new Student(2, "Tom", price + 2));
		students.add(new Student(3, "Tom", price + 3));
		return students;
	}

	//测试中Map的使用出现异常,暂未解决。可能是由于JDK的版本造成的,测试的时候使用了JDK8
	@Override
	public Map<Integer, Student> getAllStudentsMap() {
		System.out.println("Server getAllStudentsMap ");
		Map<Integer, Student>map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "umgsai", 15000));
		map.put(2, new Student(2, "umgsai2", 16000));
		map.put(3, new Student(3, "umgsai3", 17000));
		return map;
	}

}

发布服务

public class WSServer {
	public static void main(String[] args) {
		String address = "http://192.168.13.232:8084/ws_cxf/datatype";
		Endpoint.publish(address, new DataTypeWSImpl());
		System.out.println("服务发布成功");
	}
}

可以在浏览器中通过 http://192.168.13.232:8084/ws_cxf/datatype?wsdl来查看发布的服务

使用CXF自带的工具来生成客户端代码

apache-cxf-3.0.1\bin wadl2java http://192.168.13.232:8084/ws_cxf/datatype?wsdl

生成客户端代码后即可编写客户端代码

package com.umgsai.datatype.client;

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

import com.umgsai.datatype.DataTypeWS;//工具来生成客户端代码
import com.umgsai.datatype.DataTypeWSImplService;//工具来生成客户端代码
import com.umgsai.datatype.Student;//工具来生成客户端代码

public class ClientTest {
	public static void main(String[] args) {
		DataTypeWSImplService factory = new DataTypeWSImplService();
		DataTypeWS dataTypeWSImplPort = factory.getDataTypeWSImplPort();
		Map<Integer, Student> allStudentsMap = (Map<Integer, Student>) dataTypeWSImplPort.getAllStudentsMap();//Map暂时有问题。等换了JDK版本再做测试。
		System.out.println(allStudentsMap);
		Object studentById = dataTypeWSImplPort.getStudentById(1);
		System.out.println(studentById);
		List<Student> studentsByPrice = dataTypeWSImplPort.getStudentsByPrice(1000);
		System.out.println(studentsByPrice);
	}
}
时间: 2024-08-26 16:37:47

WebService学习笔记-CXF支持的数据类型的相关文章

WebService学习笔记-CXF添加拦截器

Webservice拦截器:在webservice请求过程中,动态操作请求和响应的数据 分类 按照所处的位置分:服务器端拦截器  客户端拦截器 按照消息的方向分:入拦截器  出拦截器 按照定义者分:系统拦截器 自定义拦截器 在服务器端添加拦截器 package com.demo; //注意引入的类一定要正确 import javax.xml.ws.Endpoint; import org.apache.cxf.interceptor.LoggingInInterceptor; import or

WebService学习笔记-CXF添加自定义拦截器

使用自定义拦截器实现用户名和密码的校验 客户端:出拦截器 服务器:入拦截器 客户端 AddUserInterceptor.java package com.demo.interceptors; import java.util.List; import javax.xml.namespace.QName; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org

WebService学习笔记-XML&Schema&HTTP

XML约束 在XML技术里,可以编写一个文档(文件)来约束一个XML文档的书写规范,这称之为XML约束 1.namespace 相当于schema文件的id 2.targetNamespace属性 用来指定schema文件的namespace的值 3.xmlns属性 引入一个约束, 它的值是一个schema文件的namespace值 4.schemaLocation属性 用来指定引入的schema文件的位置 schema规范中: 1. 所有标签和属性都需要有schema文件来定义 2. 所有的s

WebService学习笔记汇总

鲁春利的工作笔记,谁说程序员不能有文艺范? WebService学习笔记(一)基础入门 http://luchunli.blog.51cto.com/2368057/1722944 WebService学习笔记(二)WDSL格式 http://luchunli.blog.51cto.com/2368057/1724887

Mysql学习笔记(二)数据类型 补充

原文:Mysql学习笔记(二)数据类型 补充 PS:简单的补充一下数据类型里的String类型以及列类型... 学习内容: 1.String类型 2.列类型存储需求 String类型: i.char与varchar char与varchar的类型相似,但是他们的保存方式和检索方式不同... char的存储结构是固定长度的存储...即指定了几个字节,那么就占用几个字节,如char(4),那么无论存入的是什么字串,那么都占用四个字节...char的 可表示长度范围为0-255的任何值,当保存的字节不

WebService学习笔记系列(二)

soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议.soap协议分为两个版本,soap1.1和soap1.2. 在学习webservice时我们有一个必备工具叫做tcpmon,该工具可以直接下载得到.使用tcpmon可以嗅探网络中传输的数据,便于我们更好的理解soap协议. 下载好tcpmon之后,打开该软件,如图简单设置 tcpmon相当于一个代理服务器,打开tcpmon后,如果把监听端口设置为9999,目标端口设置为8888,当用户访问9999端口时,消息会被tcp

Mysql学习笔记(一)数据类型

学习内容: Mysql基本数据类型. 1.数字类型.. i.整型     Mysql数据类型             含义(有符号)     tinyint(m) 一个字节 范围(-128~127)     smallint(m) 两个字节 范围(-32768~32767)     mediumint(m) 三个字节 范围(-8388608~8388607)     int(m) 四个字节 范围(-2147483648~2147483647)     bigint(m) 八个字节 范围(+-9.

WebService学习笔记-使用CXF发布Webservice

WeB项目结构如图 User.java实体类 public class User { private String username; private String description; //... } HelloWorld.java接口 @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text);     String sayHiToUser(User user

WebService学习笔记(三)wsdl2java代码生成

鲁春利的工作笔记,谁说程序员不能有文艺范? WebService只是向客户端暴露WSDL,客户端需要将WSDL转换为相应编程语言书写的代码.JAX-WS的各种实现都提供相应的工具进行WSDL与JAVA之间的相互转换,在CXF的bin目录下提供了多种转换的支持. E:\mvtech\apache-cxf-3.1.4\bin>ls -l|grep bat -rwxrwxrwx   1 user     group        1815 Oct 31 04:06 idl2wsdl.bat -rwxr