- 浏览: 250285 次
- 性别:
- 来自: 苏州
文章分类
- 全部博客 (289)
- java (72)
- oracle (3)
- mysql (5)
- spring (28)
- hibernate (2)
- osgi (0)
- linux (2)
- ExtJs (1)
- jvm (0)
- mybatis (7)
- 分布式 (11)
- MINA (6)
- apache+tomcat (13)
- js+htm (7)
- android (44)
- http (1)
- hbase+hdoop (0)
- memcache (13)
- search (27)
- 部署及性能 (12)
- mongoDB (2)
- 多线程 (12)
- 安全管理验证 (9)
- struts (1)
- webservice (0)
- easyUI (1)
- spring security (16)
- pattern (6)
- 算法 (2)
最新评论
-
lzh8189146:
CommonsHttpSolrServer这个类,现在是不是没 ...
CommonsHttpSolrServer -
xiaochanzi:
我按照你的方法试了下,tomcat6可以发布,但是访问任何网页 ...
基于内嵌Tomcat的应用开发 -
phoneeye:
麻烦你,如果是抄来的文章,请给出来源。谢谢
ant 两则技巧 -
neverforget:
转载不注明出处
Spring Security3.1登陆验证 替换 usernamepasswordfilter -
liang1022:
若不使用eclipse ,如何在命令行下 运行服务端程序 ?
WebService CXF学习(入门篇2):HelloWorld
前一节仅仅只讲了与Spring整合,没有涉及到数据库,而且是直接将Java象传递到服务端。这一节我起到一个回顾前面章节的作用。用在客户端运用JABX将JAVA对象编组成XML文件,在客户端将XML解组成JAVA并存入数据库。下面我们就着手开发这个Demo:
服务端开发
第一步,编写数据资源层相关接口
DAO编写:
- public interface HibernateDao {
- public void save(UserInfo object);
- public void delete(UserInfo object);
- public void update(UserInfo object);
- public UserInfo get(int id);
- }
DAO实现类:
- public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao {
- public void delete(UserInfo object) {
- this.getHibernateTemplate().delete(object);
- }
- public void save(UserInfo object) {
- this.getHibernateTemplate().save(object);
- }
- public void update(UserInfo object) {
- this.getHibernateTemplate().update(object);
- }
- public UserInfo get(int id) {
- return (UserInfo) this.getHibernateTemplate().get(UserInfo.class, id);
- }
- }
实体对象:
- @Entity
- @Table(name = "userinfo")
- public class UserInfo implements java.io.Serializable {
- private static final long serialVersionUID = 2281022190032353574L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Integer id;
- private String name;
- private Integer age;
- private Integer address;
- public UserInfo() {
- }
- public UserInfo(String name, Integer age) {
- this.name = name;
- this.age = age;
- }
- public UserInfo(String name, Integer age, Integer address) {
- this.name = name;
- this.age = age;
- this.address = address;
- }
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return this.age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Integer getAddress() {
- return this.address;
- }
- public void setAddress(Integer address) {
- this.address = address;
- }
- }
第二步:业务逻辑层接口编写
业务逻辑接口:
- @WebService
- public interface IService {
- //public void save(@WebParam(name="info")String xml);
- public void save(@WebParam(name="dto")UserInfoDTO dto,@WebParam(name="flag")boolean flag);
- public void update(@WebParam(name="info")String xml);
- public void delete(@WebParam(name="id")int id);
- public @WebResult(name="String")String get(@WebParam(name="id")int id);
- }
接口实现类:
- @WebService
- public class ServiceImpl implements IService {
- private Logger log = LoggerFactory.getLogger(ServiceImpl.class);
- private HibernateDao dao;
- // public void setDao(HibernateDao dao) {
- // this.dao = dao;
- // }
- public void delete(int id) {
- log.info("delete id is {} user"+id);
- //UserInfo userInfo = (UserInfo)object;
- //log.info("this username is:"+userInfo.getName());
- //log.info("delete {} information..."+userInfo.getName());
- //dao.delete(userInfo);
- }
- // public void save(String xml) {
- // //UserInfo userInfo = (UserInfo)object;
- // log.info("add {} user..."+xml);
- // //dao.save(userInfo);
- // System.out.println("ssss");
- // }
- public void save(UserInfoDTO dto,boolean flag) {
- System.out.println("name:"+dto.getName());
- }
- public void update(String xml){
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext-resources.xml");
- this.dao = (HibernateDao)context.getBean("dao");
- try {
- JAXBContext jaxbContext = JAXBContext.newInstance("com.itdcl.model");
- Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
- StringReader reader = new StringReader(xml);
- Customers cus = (Customers)unmarshaller.unmarshal(reader);
- Customer cust = cus.getCustomer();
- UserInfo userInfo = new UserInfo();
- System.out.println("Address:"+cust.getAddress());
- userInfo.setAddress(cust.getAddress());
- System.out.println("Age:"+cust.getAge());
- userInfo.setAge(Integer.valueOf(cust.getAge()));
- System.out.println("Name:"+cust.getName());
- //userInfo.setId(2);
- userInfo.setName(cust.getName());
- dao.save(userInfo);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- public String get(int id){
- //return dao.get(id);
- return null;
- }
- }
DAO与Service接口配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <!--*******************Hibernate数据库持久层配置文件***********************-->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="${driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- ${hibernate.dialect}
- </prop>
- <prop key="hibernate.show_sql">${show_sql}</prop>
- <prop key="hibernate.format_sql">${format_sql}</prop>
- <!-- 缓存设置 -->
- <prop key="cache.use_query_cache">
- ${cache.use_query_cache}
- </prop>
- <prop key="cache.use_second_level_cache">
- ${cache.use_second_level_cache}
- </prop>
- <prop key="cache.provider_class">
- ${cache.provider_class}
- </prop>
- <!-- C3P0连接池配置 -->
- <prop key="c3p0.acquire_increment">
- ${c3p0.acquire_increment}
- </prop>
- <prop key="c3p0.max_statements">
- ${c3p0.max_statements}
- </prop>
- <prop key="c3p0.min_size">${c3p0.min_size}</prop>
- <prop key="c3p0.max_size">${c3p0.max_size}</prop>
- <prop key="c3p0.timeout">${c3p0.timeout}</prop>
- <prop key="c3p0.idle_test_period">
- ${c3p0.idle_test_period}
- </prop>
- </props>
- </property>
- <property name="namingStrategy">
- <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
- </property>
- <property name="packagesToScan" value="com.itdcl.pojo" />
- </bean>
- <bean id="dao" class="com.itdcl.dao.HibernateDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
WebService接口发布:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <bean id="serverUserInfo" class="com.itdcl.model.UserInfoDTO" />
- <jaxws:endpoint id="helloWorld"
- implementor="com.itdcl.service.HelloWorldImpl" address="/HelloWorld" />
- <jaxws:endpoint id="myTest"
- implementor="com.itdcl.service.MyTestImpl" address="/MyTest" />
- <jaxws:endpoint id="service"
- implementor="com.itdcl.service.ServiceImpl" address="/Service">
- <!-- jaxws:serviceFactory>
- <ref bean="jaxWsServiceFactoryBean" />
- </jaxws:serviceFactory>-->
- </jaxws:endpoint>
- <bean id="jaxWsServiceFactoryBean"
- class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
- <property name="wrapped" value="true" />
- <property name="dataBinding" ref="aegisBean" />
- </bean>
- <bean id="aegisBean"
- class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
- </beans>
第三步:服务端程序编写了,打包部署
客户端开发
第一步:WebService客户端生成,可以手工编写。这里我就通过配置Ant文件来生成WebService客户端:
- <?xml version="1.0"?>
- <project name="cxf" basedir="." default="usage">
- <property name="webroot.dir" value="E:/workspace3/CXFSpring" />
- <!-- webservice接口 -->
- <property name="target.class" value="com.itdcl.service.IService" />
- <property name="wsdl.name" value="Service.wsdl" />
- <property name="src.dir" value="src" />
- <property name="client.dir" value="client" />
- <property name="server.dir" value="server" />
- <property name="compile.dir" value="${webroot.dir}/bin" />
- <target name="usage">
- <echo
- message="java2wsdl -->java文件生成wsdl文件,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dsrc=com.starit.cxf.java2wsdl.Hello" />
- <echo
- message="wsdl2javaClient -->java文件生成client,java2wsdl -Ddir=com/starit/cxf/java2wsdl -Dwsdl=hello.wsdl" />
- </target>
- <target name="java2wsdl">
- <echo message="生成wsdl文件" />
- <exec dir="${compile.dir}" executable="cmd.exe">
- <arg line="/c java2ws" />
- <arg line="-o ${wsdl.name} -wsdl ${target.class}" />
- </exec>
- <!-- copy资源文件 -->
- <move todir="${src.dir}" preservelastmodified="true">
- <fileset dir="${compile.dir}">
- <include name="**/*.wsdl" />
- </fileset>
- </move>
- </target>
- <target name="wsdl2javaClient">
- <echo message="生成java client文件" />
- <mkdir dir="${client.dir}" />
- <exec dir="." executable="cmd.exe">
- <arg line="/c wsdl2java" />
- <!-- 生成的java所存目录 -->
- <arg line="-d ${client.dir}"/>
- <!-- wsdl文件所在路径 -->
- <arg line="-client src\*.wsdl" />
- </exec>
- </target>
- </project>
第二步:WebService接口注册配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <!-- jaxws:client id="helloWorldClient"
- address="http://localhost:9999/cxf/HelloWorld"
- serviceClass="com.itdcl.service.IHelloWorld" />-->
- <!-- jaxws:client id="myTestClient"
- address="http://localhost:9999/cxf/MyTest"
- serviceClass="com.itdcl.service.IMyTest" />-->
- <jaxws:client id="service"
- address="http://localhost:9999/cxf/Service"
- serviceClass="com.itdcl.service.IService" />
- </beans>
剩下的事就测试我们整合是否成功了,编写一个JUnit测试类或写一个Main函数测试一下。
- public class JaxbSampleClient {
- private final static String MODEL = "com.itdcl.model";
- public static void main(String[] args) throws FileNotFoundException, JAXBException, ParserConfigurationException, TransformerException {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "beans.xml");
- IService service = (IService) context.getBean("service");
- ObjectFactory factory = new ObjectFactory();
- Customer customer = factory.createCustomer();
- customer.setId(1);
- customer.setAge("26");
- customer.setName("Josen");
- customer.setAddress(100);
- Customers customers = factory.createCustomers();
- customers.setCustomer(customer);
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
- // 转换
- JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
- Marshaller marshaller = jaxbContext.createMarshaller();
- //将customers数据与doc绑定
- marshaller.marshal(customers, doc);
- DOMSource domSource = new DOMSource(doc);
- StringWriter writer = new StringWriter();
- StreamResult result = new StreamResult(writer);
- TransformerFactory tf = TransformerFactory.newInstance();
- Transformer transformer = tf.newTransformer();
- transformer.transform(domSource, result);
- String xmlString = writer.toString();
- //System.out.println(xmlString);
- service.update(xmlString);
- }
- }
运行一下,看插入数据库没有。这节到此就靠一段落啦,下节讲解WS-Security
发表评论
-
可伸缩性最佳实践:来自eBay的经验
2012-02-18 11:38 696在eBay,可伸缩性是 ... -
WebService CXF学习(高级篇3):WS-Security
2012-03-20 08:15 695这一节我们来探讨一下WebService安全问题,如果所有系 ... -
WebService CXF学习(高级篇1):整合Spring框架
2012-03-27 08:13 878通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今 ... -
WebService CXF学习(进阶篇4):JAXB剖析
2012-03-27 08:13 841前面几节我们讲解对象传递,但是通常情况下我们不直接传对象,因 ... -
WebService CXF学习(进阶篇3):对象传递
2012-03-27 08:13 710前面几节都是讲一些理论知识,现在又用一个例子来说明一下,这一 ... -
WebService CXF学习(进阶篇2):JAX-WS讲解
2012-02-15 12:39 661JAX-WS规范是一组XML web services的JAV ... -
WebService CXF学习(进阶篇1):SOAP讲解
2012-02-15 12:39 816SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP ... -
WebService CXF学习(入门篇3):WSDL描述
2012-02-15 12:39 857由于网上有很多相关这 ... -
WebService CXF学习(入门篇2):HelloWorld
2012-02-15 12:39 1140理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloW ... -
WebService CXF学习(入门篇1):CXF由来
2012-02-15 12:38 649WebService介绍 WebService让一个程序 ...
相关推荐
CXF+Spring+Hibernate实现WebService,实现的方法比较简单,只有一个增加的方法,没有用到Struts 有服务端代码和客户端测试代码
做开发时,查了大量资料,发现一比较全面的资料,分享一下!...WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF+Spring+Hibernate 3.WS-Security WebService CXF学习——JAXB剖析
整合了Spring Hibernate CXF 对于WebService的开发,使用的DB是Mysql,主要就是做了一个Demo
springMVC+hibernate+webservice
cxf+spring+hibernate集成整合jar包
完整的cxf+hibernate+spring实现 weblogic 和tomcat上完美兼容
这是个采用apache cxf实现webservice的示例,主要由spring整合hibernate、struts2实现webservice,里面struts2用到得较少……对于学习很有帮助,另外我觉得若是初学者,不过采用那方式,现在按照套方案做熟练后再去...
是eclipse导出的war文件,包含了资源。开发环境jdk1.7 tomcat7。 通过了测试,对应的文档:http://blog.csdn.net/biboheart/article/details/9148651, 有文档了,这里就不多说了。
cxf spring Hibernate 搭建webservice,在myEclipse中成功运行。数据库为mysql,sql文件在项目里。所需jar包在图片中,根据图片自己添加相应jar包。
使用spring集成cxf,在两个web ...server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外所有jar 内附 使用文档,数据库脚本
本项目结合springMVC(4.0.3.RELEASE) + spring + hibernate(4.3.5.Final) + CXF(2.6.8)+ mysql + jquery等技术,并以Maven形式实现。具体包含以下主要功能点: 1.用CXF实现Webservice和Restful服务 2.分别使用...
使用spring+cxf发布webservice,集成了hibernate
网上资料大多是只有spring+xfire 含有Hibernate的少 3.配置时候还遇到spring实现 客户端调用服务器端时候 注入失败 4.在服务器端向客户端做映射的时候失败 5.在服务器端获取非基本类转化问题
CXF环境配置 - jackymin CXF与Spring的集成 log4j的配置使用 Spring XML配置十二个最佳实践WebService开发实例 (Xfire+Spring+Hibernate)
本源码提供了一个基于Spring MVC、CXF和Hibernate的Web服务与数据库操作设计。项目包含53个文件,其中包括15个XML文件、13个Java源文件、10个属性文件、3个YAML文件、3个JSP页面、2个MF文件、1个Gitignore文件、1个...
使用spring和cxf发布webservice,并将springmvc和hibernate集成了,用的是c3p0数据源和2级缓存
CXF Webservice 整合Spring+Hibernate,实现Webservice开发
完整代码,包括jar包,下载即可运行!有什么不妥的,欢迎大家评论批评!
3、该项目采用了struts2 hibernate spring和 spring data jpa 开源框架完成,并融入了cxf开源webservice框架的应用,而这些技术都是当下流行的技术。 4、在缓存方面运用了互联网的流行技术redis实现缓存存贮,...
浪曦航母战斗编队式企业级项目培训系列明细 JAVA,JSP/Servlet基础 Struts2 ...Mule Esb发布基于CXF,Axis的WebService, Extjs3.2与Html/css/js整合打造不规则九宫格效果 JBPM4.4开发销售工作流