Xfire Spring Hibernate 发布WebService

Xfire Spring Hibernate 发布WebService

Xfire Spring 1.2 Hibernate 3 发布WebService

一、准备工作

1、下载安装Eclipse Version: 3.3.1.1 和MyEclipse6.0.1

2、下载安装MySQL Server 5.1

3、下载MySQL驱动 mysql-connector-java-5.0.8-bin.jar

4、 使用MySQL管理工具创建数据库easyLife和一张user表。我使用的是EMS SQL Manager 2007 for MySQL。

表结构如下:

id


integer

username


Varchar(20)

userpwd


Varchar(20)

插入一条数据待测试用。

5、安装Tomcat5.5

二、实例代码

连接数据库

打开MyEclipse Database Explorer 新建一个数据连接如图:

clip_image002

创建项目

1、 打开MyEclipse 新建一个Web Service Project项目输入项目名称XfireWebService。

clip_image004

2、 下一步默认

clip_image006

3、 下一步,勾选XFire 1.2 HTTP Client Libraries。

clip_image008

4、 点击View and edit libraries 去掉spring-1.2.6.jar 这个包(不去掉会和后面添加的Spring冲突)

5、 右键添加MyEclipse的Spring Capabilities,方便起见选上所有的包,选择Copy checked…,下一步默认完成。

clip_image010

6、 再添加Hibernate Capabilities,同样选上所有包。

clip_image012

下一步

clip_image014

下一步

clip_image016

下一步

clip_image018

下一步去掉复选框完成。

clip_image020

点击Replace替换所有包

clip_image022

7、 然后配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/classes/applicationContext.xml

classpath:org/codehaus/xfire/spring/xfire.xml

</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<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>/services/*</url-pattern>

</servlet-mapping>

</web-app>

8、 建立一个com.easylife包,再新建一个webservice

clip_image024

下一步

clip_image026

下一步填入如图内容,完成。

clip_image028

9、 修改ILoginDAO

package com.easylife;

//Generated by MyEclipse

public interface ILoginDAO {

public boolean login(String username, String userpwd);

}

修改LoginDAOImpl

package com.easylife;

import java.util.List;

import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

//Generated by MyEclipse

public class LoginDAOImpl extends HibernateDaoSupport implements ILoginDAO {

@SuppressWarnings("unchecked")

public boolean login(String username, String userpwd) {

boolean flag = false;

String hql = "FROM User AS u WHERE u.username=? AND u.userpwd=?";

Query q = super.getSession().createQuery(hql);

q.setString(0, username);

q.setString(1, userpwd);

List all = q.list();

if (all.size() > 0) {

flag = true;

//SysUsers t = (SysUsers) all.get(0);

}

return flag;

}

}

10、 打开MyEclipse Data Explorer视图找到user表使用Hibernate Reverse Engineering

clip_image030

下一步选择

clip_image032

下一步默认完成

11、 在applicationContext.xml 末尾添加一个bean

<bean id="LoginDAOImpl" class="com.easylife.LoginDAOImpl">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

12、 在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>

<bean

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="urlMap">

<map>

<entry key="/Login"><!--此为服务名-->

<ref bean="login" />

</entry>

</map>

</property>

</bean>

<bean id="login"

class="org.codehaus.xfire.spring.remoting.XFireExporter">

<property name="serviceFactory">

<ref bean="xfire.serviceFactory" />

</property>

<property name="xfire">

<ref bean="xfire" />

</property>

<property name="serviceBean">

<ref bean="LoginDAOImpl" /><!--在 applicationContext.xml 中装配的类-->

</property>

<property name="serviceClass">

<value>com.easylife.ILoginDAO</value><!-- 接口-->

</property>

</bean>

</beans>

13、 部署项目

clip_image034

14、 测试服务

http://localhost:8080/XfireWebService/services/Login?wsdl

返回

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions targetNamespace="http://easylife.com" xmlns:tns="http://easylife.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<wsdl:types>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://easylife.com">

<xsd:element name="login">

<xsd:complexType>

<xsd:sequence>

<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>

<xsd:element maxOccurs="1" minOccurs="1" name="in1" nillable="true" type="xsd:string"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name="loginResponse">

<xsd:complexType>

<xsd:sequence>

<xsd:element maxOccurs="1" minOccurs="1" name="out" type="xsd:boolean"/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>

</wsdl:types>

<wsdl:message name="loginResponse">

<wsdl:part name="parameters" element="tns:loginResponse">

</wsdl:part>

</wsdl:message>

<wsdl:message name="loginRequest">

<wsdl:part name="parameters" element="tns:login">

</wsdl:part>

</wsdl:message>

<wsdl:portType name="ILoginDAOPortType">

<wsdl:operation name="login">

<wsdl:input name="loginRequest" message="tns:loginRequest">

</wsdl:input>

<wsdl:output name="loginResponse" message="tns:loginResponse">

</wsdl:output>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="ILoginDAOHttpBinding" type="tns:ILoginDAOPortType">

<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="login">

<wsdlsoap:operation soapAction=""/>

<wsdl:input name="loginRequest">

<wsdlsoap:body use="literal"/>

</wsdl:input>

<wsdl:output name="loginResponse">

<wsdlsoap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="ILoginDAO">

<wsdl:port name="ILoginDAOHttpPort" binding="tns:ILoginDAOHttpBinding">

<wsdlsoap:address location="http://localhost:8080/XfireWebService/services/Login"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

15、 JAVA调用WebService

package com.easylife;

import java.net.MalformedURLException;

import java.util.Iterator;

import java.util.List;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.easylife.User;

import com.easylife.ILoginDAO;

public class UserServiceWSTest {

public static void main(String[] args) throws Exception {

// 创建服务实例

Service srvcModel = new ObjectServiceFactory()

.create(ILoginDAO.class);

XFireProxyFactory factory = // 创建代理工厂实例

new XFireProxyFactory(XFireFactory.newInstance().getXFire());

String helloWorldURL = "http://localhost:8080/XfireWebService/services/Login";

ILoginDAO srvc = null;

try { // 获取指定位置的web服务对象

srvc = (ILoginDAO) factory.create(srvcModel, helloWorldURL);

} catch (MalformedURLException e) {

e.printStackTrace();

}

// 调用只返回String的方法.在接口名.aegis.xml中不用设置方法名

boolean result = srvc.login("admin","123");

System.out.println(result);

}

}

Run As Java Application返回True

完毕

转自:http://moluobuzai.spaces.live.com/blog/cns!7F741380D64704BA!276.trak
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值