您的位置:首页 > 编程语言 > Java开发

使用CXF框架学习搭建WebService(二)

2015-09-16 15:18 645 查看
在原有上篇文章的基础之上继续进行深入学习,函数调用中使用CXF支持的数据类型例如JavaBean或者List等 

注:本文使用的环境为JDK 1.8.0_45, Eclipse Mars 4.5 , CXF为3.1.2 

先说一下程序的需求:项目用于描述学生手中的书籍对应关系,建立2个JavaBean一个为学生类Student(ID,姓名)一个为Book书籍(书名、作者、售价、出版社) ,并且为了在此省去访问数据库的复杂的过程,通过在HelloWorldImpl类中建立静态Map<Student, Book> 同时使用静态代码块来初始化Map数据(此处在实际项目中需使用JDBC或Hibeernate来处理),具体过程如下:

1. 建立Book JavaBean,同时生成Get,Set方法及带参构造方法

public class Book {
private String name;
private String author;
private double price;
private String publisher;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}

public Book() {
// TODO Auto-generated constructor stub
super();
}

public Book(String name, String author, double price, String publisher) {
super();
this.name = name;
this.author = author;
this.price = price;
this.publisher = publisher;
}
}


2. 创建Student JavaBean,同时Get, Set方法及构造函数等(覆盖hashCode和equals方法, 只要类中userId相同则认为是同一对象)

public class Student {
private String userId;
private String userName;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}
public Student() {
// TODO Auto-generated constructor stub
super();
}

public Student(String userId, String userName) {
super();
this.userId = userId;
this.userName = userName;
}
}

3. 修改HelloWorld接口增加通过学生对象访问对应List<Book>的方法

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

import javax.jws.WebService;

@WebService		//标记此接口为WebService使用的注解,此行必不可少,没有它WebService将无法正常工作
public interface HelloWorld {
public String SayHi(String name);
public List<Book> getBookByStu(Student student);<span style="white-space:pre">	//通过学生Bean对象查询学生手中List<Book>的方法</span>
}


4. HelloWorldImpl类中增加静态Map成员变量,以及静态代码块初始化静态Map成员变量

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

import javax.jws.WebService;

@WebService 	//标记此实现类为WebService使用的注解,此行必不可少,没有它WebService将无法正常工作
public class HelloWorldImpl implements HelloWorld {

static final Map<Student, List<Book>> Db = new HashMap<Student,List<Book>>();

static {	//模拟学生中对应手中的书的数据库模拟
List<Book> list1 = new ArrayList<Book>();
list1.add(new Book("JavaSE 从入门到精通", "张孝祥", 25, "电子工业出版社"));
list1.add(new Book("JavaScript入门", "毕向东", 20, "电子工业出版社"));

List<Book> list2 = new ArrayList<Book>();
list2.add(new Book("JavaWeb21天学通", "王昭珽", 30, "清华大学出版社"));
list2.add(new Book("Strus2+Hibernate+Spring自学通", "李刚", 40, "北京大学出版社"));

Db.put(new Student("wb", "王斌"), list1);
Db.put(new Student("zs", "zhangsan"), list2);
}

@Override
public String SayHi(String name) {
// TODO Auto-generated method stub
return "Hello " + name;
}

public List<Book> getBookByStu(Student student) {<span style="white-space:pre">	//通过学生Bean对象查询学生手中List<Book>的方法</span>实现
return Db.get(student);
}
}
5. 测试类WebServiceDemo则无需进行更改直接运行即可然后通过地址进行访问WSDL

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://heima.it/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWorldImplService" targetNamespace="http://heima.it/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://heima.it/" elementFormDefault="unqualified" targetNamespace="http://heima.it/" version="1.0">

<xs:element name="SayHi" type="tns:SayHi"/>

<xs:element name="SayHiResponse" type="tns:SayHiResponse"/>

<xs:element name="getBookByStu" type="tns:getBookByStu"/>

<xs:element name="getBookByStuResponse" type="tns:getBookByStuResponse"/>

<xs:complexType name="getBookByStu">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="tns:student"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="student">
<xs:sequence>
<xs:element minOccurs="0" name="userId" type="xs:string"/>
<xs:element minOccurs="0" name="userName" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="getBookByStuResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:book"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="book">
<xs:sequence>
<xs:element minOccurs="0" name="author" type="xs:string"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element name="price" type="xs:double"/>
<xs:element minOccurs="0" name="publisher" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="SayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="SayHiResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>

</xs:schema>
</wsdl:types>
<wsdl:message name="getBookByStu">
<wsdl:part element="tns:getBookByStu" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="getBookByStuResponse">
<wsdl:part element="tns:getBookByStuResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="SayHi">
<wsdl:part element="tns:SayHi" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="SayHiResponse">
<wsdl:part element="tns:SayHiResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="getBookByStu">
<wsdl:input message="tns:getBookByStu" name="getBookByStu">
</wsdl:input>
<wsdl:output message="tns:getBookByStuResponse" name="getBookByStuResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="Sa
4000
yHi">
<wsdl:input message="tns:SayHi" name="SayHi">
</wsdl:input>
<wsdl:output message="tns:SayHiResponse" name="SayHiResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="<span style="color:#ff0000;">getBookByStu</span>">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getBookByStu">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getBookByStuResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SayHi">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="SayHi">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="SayHiResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldImplService">
<wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://192.168.0.117:666/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
可以看到这次WSDL文档中包含了新增的getBookByStu方法(红色字体标出) 至此服务器端的修改已完成

下一步是针对客户端的程序做修改

1. 同样通过WSDL2JAVA方法生成对应的JAVA类,打开Eclipse刷新可以看到GetBookByStu等新增的类



2. 客户端测试类ClientDemo进行修改

import java.util.Iterator;

public class ClientDemo {
public static void main(String[] args) {
//生成服务工厂
HelloWorldImplService factory = new HelloWorldImplService();
//产生服务对象
HelloWorld hw = factory.getHelloWorldImplPort();
//打印调用sayHi方法的结果
System.out.println("sayHi的查询结果:" + hw.sayHi("zhangsan"));

//建立student实例类,用于查询此学生手中的书籍
Student student = new Student();
student.setUserId("wb");	//因为在服务器端已经更改了hashCode和equals方法故无需设置name属性,只要id相同则认为同一对象
//依次打印查询结果
System.out.println("学生wb手中的书籍有:");
//测试使用getBookByStu方法类
for(Iterator<Book> it = hw.getBookByStu(student).iterator();it.hasNext();) {
Book book = it.next();
System.out.println("书名:" + book.getName() + " 作者:" + book.getAuthor() +
" 售价:" + book.getPrice() + " 出版社:" + book.getPublisher());
}
}
}

运行结果如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse java webservice