网上介绍XFire方面的文章已经很多了,我主要是介绍一下与Spring开发的集成,做个例子,抛块砖吧。
个人感受:先谈个人感受,可以更加明确和了解开发中的思路,XFire与Spring可以说得上绝配,如果你开发所采用的架构是SSH模式,或者有Spring框架,那么你就正常开发,不要管web service,你只需要明白哪个类是用于web service的,并且在Spring测试通过,你的web service服务端开发可以说基本搞定。
说明:
我写这文章主要是说明开发过程及给个例子,并不是讲web service 协议或者spring开发。我有时间可以写点这方面的内容。
开发环境:
1,JDK 1.6.0
2,Eclipse JEE 3.4,外加XFire eclipse plugin(主要用于客户端开发)。
3,tomcat-5.5.26
4,SoapUI 主要用于测试。
5,下载xfire-distribution-1.2.6.zip,pjl-comp-filter-1.6.6.zip(用于传输压缩)
需求:
对外提供一个表的增删改查。
网上介绍XFire方面的文章已经很多了,我主要是介绍一下与Spring开发的集成,做个例子,抛块砖吧。
个人感受:先谈个人感受,可以更加明确和了解开发中的思路,XFire与Spring可以说得上绝配,如果你开发所采用的架构是SSH模式,或者有Spring框架,那么你就正常开发,不要管web service,你只需要明白哪个类是用于web service的,并且在Spring测试通过,你的web service服务端开发可以说基本搞定。
说明:
我写这文章主要是说明开发过程及给个例子,并不是讲web service 协议或者spring开发。我有时间可以写点这方面的内容。
开发环境:
1,JDK 1.6.0
2,Eclipse JEE 3.4,外加XFire eclipse plugin(主要用于客户端开发)。
3,tomcat-5.5.26
4,SoapUI 主要用于测试。
5,下载xfire-distribution-1.2.6.zip,pjl-comp-filter-1.6.6.zip(用于传输压缩)
需求:
对外提供一个表的增删改查。
开发步骤:
1,建立web开发环境。
2,拷贝需要采用的依赖包。
3,开发业务功能。
4,配置Spring。
5,配置web service。
6,测试。
开发:
1,建立web开发环境。
用eclipse建立一个动态web项目(myeclipse也一样,需要配置tomcat用于提供web service)。
2,拷贝需要采用的依赖包。
解压xfire-distribution-1.2.6.zip,pjl-comp-filter-1.6.6.zip拷贝lib里面的内容及xfire-all-1.2.6.jar,pjl-comp-filter-1.6.6.jar,ORACLE驱动classes12.jar。
3,开发业务功能。
建立表结构
create table HD.HDUSER
(
ID NUMBER(8) not null,
NAME NVARCHAR2(16) not null,
PASSWD NVARCHAR2(16)
)
tablespace HANDLEWELL
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
|
建立业务对象
public class User implements Serializable {
private static final long serialVersionUID = -4924323782825351658L;
private Integer id;
private String name;
private String passwd;
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 String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer("User:[");
sb.append("ID:").append(id).append(",");
sb.append("Name:").append(name).append(",");
sb.append("Passwd:").append(passwd).append("]");
return sb.toString();
}
}
|
建立业务接口
public interface IUserService {
public boolean addUser(int id, String name, String passwd);
public boolean modifyUser(int id, String name, String passwd);
public User queryUser(int id);
public boolean deleteUser(int id);
}
|
建立DAO接口
public interface IUserDAO {
public boolean addUser(User user);
public boolean modifyUser(User user);
public User queryUser(int id);
public boolean deleteUser(int id);
}
|
实现DAO接口
public class UserDAOImpl extends JdbcDaoSupport implements IUserDAO {
@Override
public boolean addUser(User user) {
String sql = "insert into HDUSER(id, name, passwd)values(" + user.getId() + ",'" + user.getName() + "','" + user.getPasswd() + "')";
this.getJdbcTemplate().execute(sql);
return true;
}
@Override
public boolean deleteUser(int id) {
String sql = "delete from HDUSER where id=" + id;
this.getJdbcTemplate().execute(sql);
return true;
}
@Override
public boolean modifyUser(User user) {
// TODO Auto-generated method stub
return false;
}
@Override
public User queryUser(int id) {
// TODO Auto-generated method stub
return null;
}
}
|
实现业务
public class UserServiceImpl implements IUserService {
private IUserDAO userDAO;
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public boolean addUser(int id, String name, String passwd) {
User u = new User();
u.setId(new Integer(id));
u.setName(name);
u.setPasswd(passwd);
this.getUserDAO().addUser(u);
return true;
}
@Override
public boolean deleteUser(int id) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean modifyUser(int id, String name, String passwd) {
// TODO Auto-generated method stub
return false;
}
@Override
public User queryUser(int id) {
// TODO Auto-generated method stub
return null;
}
}
|
4,配置Spring。
在WEB-INF下建立applicationContext.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="userBean" class="com.handlewell.user.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAO" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="jdbcUrl"><value>jdbc:oracle:thin:@localhost:1521:orcl</value></property>
<property name="user"><value>hd</value></property>
<property name="password"><value>hd</value></property>
</bean>
<bean id="userDAO" class="com.handlewell.user.dao.impl.UserDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
|
在web.xml文件加入Spring配置
<!-- begin Spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- end Spring配置 -->
|
5,配置web service。
在WEB-INF下建立xfire-servlet.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/userService.ws">
<ref bean="userService" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
</bean>
<!-- 建立web service服务 -->
<bean id="userService" parent="baseWebService">
<property name="serviceBean" ref="userBean" />
<property name="serviceClass" value="com.handlewell.user.service.IUserService" />
</bean>
</beans>
|
在web.xml中加入
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<!-- end XFire配置 -->
|
至此开发工作结束。
6,测试。
启动tomcat服务,先访问一下WSDL信息http://localhost:8080/sx/userService.ws?wsdl
生成的WSDL信息如下
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://service.user.handlewell.com"
xmlns:ns1="http://entity.user.handlewell.com" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"
xmlns:tns="http://service.user.handlewell.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://service.user.handlewell.com">
<xsd:element name="modifyUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0"
type="xsd:int" />
<xsd:element maxOccurs="1" minOccurs="1" name="in1"
nillable="true" type="xsd:string" />
<xsd:element maxOccurs="1" minOccurs="1" name="in2"
nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="modifyUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out"
type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="addUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0"
type="xsd:int" />
<xsd:element maxOccurs="1" minOccurs="1" name="in1"
nillable="true" type="xsd:string" />
<xsd:element maxOccurs="1" minOccurs="1" name="in2"
nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="addUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out"
type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="queryUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0"
type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="queryUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out"
nillable="true" type="ns1:User" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="deleteUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0"
type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="deleteUserResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out"
type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://entity.user.handlewell.com">
<xsd:complexType name="User">
<xsd:sequence>
<xsd:element minOccurs="0" name="id" nillable="true"
type="xsd:int" />
<xsd:element minOccurs="0" name="name" nillable="true"
type="xsd:string" />
<xsd:element minOccurs="0" name="passwd" nillable="true"
type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="queryUserResponse">
<wsdl:part name="parameters" element="tns:queryUserResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="modifyUserRequest">
<wsdl:part name="parameters" element="tns:modifyUser">
</wsdl:part>
</wsdl:message>
<wsdl:message name="addUserResponse">
<wsdl:part name="parameters" element="tns:addUserResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="modifyUserResponse">
<wsdl:part name="parameters" element="tns:modifyUserResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="deleteUserResponse">
<wsdl:part name="parameters" element="tns:deleteUserResponse">
</wsdl:part>
</wsdl:message>
<wsdl:message name="deleteUserRequest">
<wsdl:part name="parameters" element="tns:deleteUser">
</wsdl:part>
</wsdl:message>
<wsdl:message name="queryUserRequest">
<wsdl:part name="parameters" element="tns:queryUser">
</wsdl:part>
</wsdl:message>
<wsdl:message name="addUserRequest">
<wsdl:part name="parameters" element="tns:addUser">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="IUserServicePortType">
<wsdl:operation name="modifyUser">
<wsdl:input name="modifyUserRequest" message="tns:modifyUserRequest">
</wsdl:input>
<wsdl:output name="modifyUserResponse" message="tns:modifyUserResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addUser">
<wsdl:input name="addUserRequest" message="tns:addUserRequest">
</wsdl:input>
<wsdl:output name="addUserResponse" message="tns:addUserResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="queryUser">
<wsdl:input name="queryUserRequest" message="tns:queryUserRequest">
</wsdl:input>
<wsdl:output name="queryUserResponse" message="tns:queryUserResponse">
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="deleteUser">
<wsdl:input name="deleteUserRequest" message="tns:deleteUserRequest">
</wsdl:input>
<wsdl:output name="deleteUserResponse" message="tns:deleteUserResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="IUserServiceHttpBinding" type="tns:IUserServicePortType">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="modifyUser">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="modifyUserRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="modifyUserResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="addUser">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="addUserRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="addUserResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="queryUser">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="queryUserRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="queryUserResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="deleteUser">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="deleteUserRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="deleteUserResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="IUserService">
<wsdl:port name="IUserServiceHttpPort" binding="tns:IUserServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8080/user/userService.ws" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
|
把内容存为文件。
启动soapUI,新建项目,并导入刚才的wsdl文件。
在addUser下request单击show request editor,就可以看到soap请求信息。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.user.handlewell.com">
<soapenv:Header/>
<soapenv:Body>
<ser:addUser>
<ser:in0>11</ser:in0>
<ser:in1>aaa</ser:in1>
<ser:in2>bbb</ser:in2>
</ser:addUser>
</soapenv:Body>
</soapenv:Envelope>
|
提交,查看数据库,数据存入即成功。
有些朋友可能在webservice中各wsdl和soap协议各自的作用,我传人图片大家就明白了。