结合spring框架来实现CXF发布SOAP协议的服务,步骤基本相同,所不同的是的多了一些配置项,步骤如下

1. 服务端

第一步:创建web项目(引入jar包)

第二步:创建SEI接口

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding; @WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface { public String QueryWeather(String cityName);
}

第三步:创建SEI实现类

public class WeatherInterfaceImpl implements WeatherInterface {

    @Override
public String QueryWeather(String cityName) {
System.out.println("from client..." + cityName);
if ("北京".equals(cityName)) {
return "晴转多云";
} else {
return "雨转小雪";
}
}
}

第四步:配置spring配置文件,applicationContext.xml,用<jaxws:server>标签发布服务,设置1.服务地址;2.设置服务接口;3.设置服务实现类

<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<jaxws:server address="/weather" serviceClass="com.zang.ws.cxf.server.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean> <!-- 配置拦截器 -->
<jaxws:inInterceptors>
<ref bean="inIntercepter"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outIntercepter"/>
</jaxws:outInterceptors>
</jaxws:server>
<!-- 配置拦截器的bean -->
<bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <!-- 配置服务实现类 -->
<bean name="weatherInterface" class="com.zang.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>

第五步:配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ws_cxf_spring_server</display-name> <!-- 设置spring的环境 -->
<context-param>
<!--contextConfigLocation是不能修改的 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置CXF的Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

第六步:部署到tomcat下,启动tomcat

第七步:测试服务,阅读使用说明书  地址: http://localhost:8089/ws_cxf_spring_server/ws/weather?wsdl

如果直接创建实现类,可以使用Endpoint标签发布服务。步骤如下

创建实现类

@WebService
public class HelloWorld {
public String sayHello(String name){
return "hello,"+name;
}
}

之前通过创建SEI接口实现时,applicationContext.xml中是用<jaxws:server>标签来发布服务;而直接通过创建类来实现时,applicationContext.xml中应使用<jaxws:endpoint>标签来发布服务。

<!-- <jaxws:endpoint发布SOAP协议的服务 ,对Endpoint类封装-->
<jaxws:endpoint address="/hello" implementor="com.zang.ws.cxf.server.HelloWorld"/>

重启tomcat,访问说明书  http://localhost:8089/ws_cxf_spring_server/ws/hello?wsdl

项目结构

2. 客户端

第一步:引入jar包

第二步:生成客户端代码  wsdl2java命令,详见客户端实现

第三步:配置spring配置文件,applicationContent.xml

<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- <jaxws:client实现客户端 ,对JaxWsProxyFactoryBean类封装 -->
<jaxws:client id="weatherClient"
address="http://127.0.0.1:8089/ws_cxf_spring_server/ws/weather"
serviceClass="com.zang.cxf.weather.WeatherInterface" />
</beans>

第四步:从spring上下文件获取服务实现类,调用查询方法,打印

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zang.cxf.weather.WeatherInterface; public class WeatheClient { public static void main(String[] args) {
// 初始化spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
     // 调用查询方法
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");
String weather = weatherInterface.queryWeather("济南");
System.out.println(weather);
}
}

项目结构

Web Service——CXF+Spring 整合的更多相关文章

  1. Web Service——CXF发布REST服务

    1. 什么是REST REST,英文representational state transfer(表象性状态转变)或者表述性状态转移,REST是web服务的一种架构风格,使用HTTP.URI.XML ...

  2. Building a RESTful Web Service Using Spring Boot In Eclipse

    一.构建restful web service 创建Maven的java web工程,maven的pom文件加入依赖包 创建包hello Greeting.java package hello; pu ...

  3. Web Service CXF的工作流程

    我们一起走进系统的内部,跟随每一个调用,去透视系统的每一个层面. 一.我们定义整个目录都在CXFServlet的监控之下 <servlet> <servlet-name>CXF ...

  4. Web Service——CXF

    1. 什么是CXF Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.Apache CXF ...

  5. 翻译-使用Spring调用SOAP Web Service

    原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...

  6. java框架之Spring(4)-Spring整合Hibernate和Struts2

    准备 导包 Struts2 导入 Struts2 zip 包解压目录下 'apps/struts-blank.war' 中所有 jar 包,如下: asm-3.3.jar asm-commons-3. ...

  7. [C#]動態叫用Web Service

    http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/22/8139.aspx 摘要 Web Service對大家來說想必都不陌生,也大都了解Web S ...

  8. Building a RESTful Web Service(转)

    Building a RESTful Web Service This guide walks you through the process of creating a "hello wo ...

  9. 【转】Building a RESTful Web Service

    目标 构建一个service,接收如下HTTP GET请求: [plain] view plain copy   http://localhost:8080/greeting 并返回如下JSON格式的 ...

随机推荐

  1. Selenium2+python自动化49-判断文本(text_to_be_present_in_element)

    前言 在做结果判断的时候,经常想判断某个元素中是否存在指定的文本,如登录后判断页面中是账号是否是该用户的用户名. 在前面的登录案例中,写了一个简单的方法,但不是公用的,在EC模块有个方法是可以专门用来 ...

  2. 学习node js 之微信公众帐号接口开发 准备工作之三

    app.js文件介绍,因为也是初学,以下的内容是个人的理解,有些不正确的地方请评论中指证:以注解的形式说明. //依赖组件[模块]导入 var express = require('express') ...

  3. Android之从Browser中打开本地的应用程序&微信检测是否有对应app

    在对应的应用程序的AndroidManifest.xml中配置: <activity android:name=".ui.TabHostActivity" android:w ...

  4. C# 模拟网站登陆

    实现此功能首先需要借助一些抓包工具,对相应的网站登陆过程进行分析,此过程根据网站的不同,可能复杂,也可能很简单.常用的抓包工具FF下FireBug和IE下的HttpWatch.这两个工具很强大,以此工 ...

  5. MICS:副本和纠删码混合存储系统

    摘要 云存储系统的三个指标: 高可靠性,低存储开销,高读写性能. 这三个指标是没有办法同一时候满足的,许多时候须要进行tradeoff. 副本系统和纠删码是两种在存储系统中广泛使用的策略,它们在保证高 ...

  6. insert-delete-getrandom-o1-duplicates-allowed

    https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ public class Randomized ...

  7. go语言基础之可见性规则验证

    1.可见性规则验证 如果想使用别的包的函数.结构体类型.络构体成员. 函数名.类型名,结构体成员变量名,首字母必段大写,可见. 如果首字母是小写,只能在同一个包里使用. 文件夹样例: 示例: vi t ...

  8. Pytorch torch.optim优化器个性化使用

    一.简化前馈网络LeNet 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 im ...

  9. 【反射】Reflect Class Field Method Constructor

    关于反射 Reflection 面试题,什么是反射(反射的概念)? 主要是指程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义 ...

  10. scala 学习笔记九 定义操作符

    Scala中方法名可以包含几乎所有字符,还可以对操作符+赋予新的含义 上面例子中136行,用下划线来表示“缺省初始化值” 上面151行和153行都是通过圆点表示法进行调用 157行和159行用中缀表示 ...