最近在负责一个大系统的实施,经过需求分析之后,将系统分为5个子系统,我们采用SOA架构,分模块开发。项目组中最大的一个争议就是,子系统之间的通讯问题,大家提出了两种方案:一、如果5个子系统最后发布为5个war包,那么相互之间就不能直接调用,而是需要通过webservices等通讯方式,那会增加一些开发工作量;二、如果5个子系统合并在一个大工程中,下面放所有的模块,那子系统间的访问很简单,但是日常的开发管理会存在比较大的风险。

从管理的角度来看,我是比较偏向第一种方案,因为这样结构更清晰更简单,开发人员之间的相互影响也较小,还有一个好处就是,可以将不同的子系统发布在不同的应用服务器上,避免了由于某一个子系统崩溃导致整个系统的崩溃。于是,我对webservices的开发技术进行了研究,发现用spring和CXF集成来发布webservice和调用webservice都非常的简单,也加大了我选择第一种方案的决心。下面就简单介绍一个spring和CXF集成的示例。

一、 准备工作。
        1、下载apache-cxf的应用包,地址:http://cxf.apache.org/download.html,我选择的是2.4.1版本。

二、发布webservices
1. 新建web project ,并加入apache-cxf-2.4.1\lib所有包,编写要发布的web service 接口和实现.这一步,与前面一样。

1)创建一个接口类,并加上webservice标记

import javax.jws.WebService;

@WebService

public interface HelloWorld {

public String sayHello(String text);

}

2)创建上面这个接口的实现类
       import javax.jws.WebService;

@WebService(endpointInterface="test.HelloWorld")

public class HelloWorldImpl implements HelloWorld {

public String sayHello(String text) {

return "Hello" + text ;

}

}

@WebService 注解表示是要发布的web 服务,endpointInterface的值是该服务类对应的接口。

2. 在spring-cxf.xml配置发布的web 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:jaxws="http://cxf.apache.org/jaxws"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="hello" class="test.HelloWorldImpl" />

<jaxws:endpoint id="helloWorld" implementor="#hello"

address="/HelloWorld" />

</beans>

注意:<jaxws:endpoint id="helloWorld" implementor="#hello"

address="/HelloWorld" />

id:指在spring配置的bean的ID.

Implementor:指明具体的实现类.

Address:指明这个web service的相对地址,

3. 配置web.xml文件:

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

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

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

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> 
                   <!--spring配置文件放在WEB-INF目录下-->

<param-value>/WEB-INF/spring-cxf.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener> 
          <servlet>

<servlet-name>CXFServlet</servlet-name>

<servlet-class>

org.apache.cxf.transport.servlet.CXFServlet

</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>CXFServlet</servlet-name>

<url-pattern>/*</url-pattern>

</servlet-mapping>

</web-app>

4.部署到tomcat服务器,输入:http://localhost:8080/<web-app-name>/HelloWorld?wsdl,将显示这个web service的wsdl.

注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name>

<url-pattern>/ws/*</url-pattern>

则访问地址为:http://localhost:8080/<web-app-name>/ws/HelloWorld?wsdl
到此webservices就发布成功了。

三、创建一个客户端来调用webservices
1、 同样新建 java project , 并加入 a pache-cxf-2.0.7\lib 所有包,添加到Build Path;
2、将webservice的接口类导出成jar包,也添加到Build Path,主要目的是客户端要用到服务端的HelloWorld这个类。如果不想导入这个jar包也可以,只要在客户端创建一个一摸一样的接口类:HelloWorld,特别要注意以下两点:
    1)接口前面要添加@Webservice的标记,不然会抛出一个 javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.
    2)包路径也要一样,不然会抛出一个ClassCastException: $Proxy29 cannot be cast to...
3、 配置 spring-client.xml

<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/schema/jaxws.xsd">

<!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->

<bean id="client" class="test.HelloWorld"

factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
     <!-- 这个class的包路径和类名和服务端提供web服务的接口一致-->

<property name="serviceClass" value="test.HelloWorld"/>
     <!-- 这个address一定要注意,正确的-->

<property name="address" value="http://localhost:8080/<web-app-name>/HelloWorld"/>

</bean>

</beans>

4.测试:

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.HelloWorld;

public class Test {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(

"spring-client.xml");

HelloWorld client = (HelloWorld) ctx.getBean("client");

String result = client.sayHello("Glen!");

System.out.println(result);

}

}

结果输出“Hello,Glen!”,测试通过,至此一个webservice的调用也成功了。

spring和CXF集成来实现webservices相关推荐

  1. 一个CXF集成SPRING的WEBSERVICE完整实例

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 1 首先 ...

  2. Spring整合CXF,发布RSETful 风格WebService

    这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了.如果你对Spring整合CXF ...

  3. Spring Boot 工程集成全局唯一ID生成器 Vesta

    2019独角兽企业重金招聘Python工程师标准>>> 本文内容脑图如下: 文章共 760字,阅读大约需要 2分钟 ! 概 述 在前一篇文章 <Spring Boot工程集成全 ...

  4. Spring和CXF整合发布WebService(服务端、客户端)

    参考Spring和CXF整合发布WebService(服务端.客户端) 转载于:https://www.cnblogs.com/timspace/p/11113576.html

  5. Spring+SpringMVC+MyBatis集成(SSM)

    1.导入需要用到的jar包 <dependencies><!--Spring核心包--><dependency><groupId>org.springf ...

  6. redis与spring的完全集成

    2019独角兽企业重金招聘Python工程师标准>>> redis与spring的完全集成 博客分类: 缓存 下载spring-data-redis,gav如下: [html]  v ...

  7. Spring系列之集成MongoDB的2种方法,你知道嘛?

    Spring系列之集成MongoDB的2种方法,你知道嘛? MongoDB是最流行的NoSQL数据库,SpringBoot是使用Spring的最佳实践.今天带大家讲一讲SpringBoot集成Mong ...

  8. 在spring boot中集成Swagger

    Swagger 在spring boot中集成Swagger 新建一个swagger项目 maven依赖 <!-- https://mvnrepository.com/artifact/io.s ...

  9. spring 项目中集成 Protocol Buffers 示例

    http://blog.csdn.net/fangzhangsc2006/article/details/8687388 本文适用于了解spring框架,同时想在spring项目中使用Protocol ...

最新文章

  1. php 图片处理库 Imagick 代替 gd
  2. python基础练习(三)
  3. 十张图了解2021年中国数据中心产业链投资现状和投资并购发展趋势
  4. Linux文件属性和权限
  5. servlet怎么接受请求_谁再问Servlet的问题,我就亲自上门来教学了
  6. c语言选择排序_C语言——选择排序
  7. android对象缓存,Android简单实现 缓存数据
  8. 静态页面被拦截解决办法
  9. JS代码实现浏览器切换页面时网页标题动态切换
  10. 引领智慧教育,联想云桌面如何打造教育“一朵云”?
  11. 计算软件介绍siesta、vasp、wien2k、PWSCF、Materials Studio
  12. kubernetes实践之三十: SonarQube和SonarQube Runner
  13. 你想要的宏基因组-微生物组知识全在这(2020.6)
  14. hr面试性格测试30题_辉瑞面试过程辉瑞性格测试题
  15. 霓虹灯(light)
  16. 计算机的未来展望英语作文,展望未来英语作文范文
  17. CTF MISC系列————5、小可爱
  18. 高校科研管理系统设计与实现小程序-计算机毕业设计
  19. 数据库的几种去重方法
  20. 计算机专业优秀计划,2020年计算机专业学习计划优秀范文2篇.doc

热门文章

  1. mysql数据库入门教程(8):数据的基本类型
  2. ArrayList 与 LinkedList 底层实现
  3. VTK:邻接矩阵到 EdgeTable用法实战
  4. JavaScript实现longest Common Substring最长公共子串算法(附完整源码)
  5. JavaScript实现ShellSort希尔排序算法(附完整源码)
  6. JavaScript实现combine Without Repetitions不重复地结合算法(附完整源码)
  7. boost::parameter::deduced相关的测试程序
  8. boost::mpi::cartesian_communicator相关用法的测试程序
  9. boost::hana::metafunction用法的测试程序
  10. boost::hana::at_c用法的测试程序