Web Service——CXF+Spring 整合

结合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);
    }
}

项目结构

原文地址:https://www.cnblogs.com/zjfjava/p/9022499.html

时间: 2024-08-02 03:38:24

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

在S2SH中添加Web Service(CXF),怎么CXF集成SSH

下面是:beanContext-spring-cxf.xml 中的配置:(一定要注意顶部引入的beans里面的spring 配置一定要和你spring的版本一样不然不行哦) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.

翻译-使用Spring调用SOAP Web Service

原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP的web service的整个过程. 指南内容 你将构建一个客户端,使用SOAP用来从远端的基于WSDL的web service获取天气数据.请访问http://wiki.cdyne.com/index.php/CDYNE_Weather进一步获取该天气服务的信息. 该服务根据邮编返回天气预测.你可

Building a RESTful Web Service(转)

Building a RESTful Web Service This guide walks you through the process of creating a "hello world" RESTful web service with Spring. What you’ll build You’ll build a service that will accept HTTP GET requests at: http://localhost:8080/greeting a

RESTful Web Service

1. 何为REST 理解RESTful架构 2. 如何创建RESTful Web Service 使用 Spring 3 来创建 RESTful Web Services 3. RESTful Web Serivce 和 SOAP Web Service 对比 Web 服务编程,REST 与 SOAP SOAP webserivce 和 RESTful webservice 对比及区别

Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)

CXF与Spring整合,分两个方面给大家介绍: 1,在传统ssh项目基础上添加Web Service 赋值CXF的jar包 在web.xml配置文件中导入CXF的核心控制器:CXFServlet 在Spring配置文件中导入CXF提供Schema,xml配置文件 在Spring配置文件中使用jaxws:endpoint元素来暴露Web Service 如果要添加拦截器,在jaxws:endpoint元素里添加 inInterceptors,outInterceptors子元素 2,远程调用We

Web Service (四) 手动发布Web Service接口-CXF与Spring集成

当我们发布完Web Service接口之后有两种方式可以调用Web service服务,一种是通过动态客户端方式,另一种是引用服务端的接口,引用服务端接口的方式对于客户端同服务器端耦合比较大,而使用WSDL的方式客户端不知道服务端的存在就可以调用服务器的方法. 下面是项目的结构图: 1.Web Service发布项目 2.编写服务端接口类以及实现类,如下,同上一篇自动发布接口,多了一个注解@WebService package com.test.webservice; import javax.

Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并引入spring来进行RESTful web service的配置和管理. 项目目录结构如下图 首先我们要在web.xml中加入通过Spring的ContextLoaderListener加载的Spring运行时环境以及CXF的Spring配置文件 web.xml <?xml version="

Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通ServletJAX-RS(RESTful) web service

起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先在eclipse中新建一个Dynamic Web Project,然后实现上篇文章中的所有类,唯一不同的是这里,我们不需要一个Server.java来启动一个Web Service.我们用CXF自带的org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServ

基于Maven在Spring中集成CXF Web Service框架

引言: 在跨系统和跨平台的系统通信中,WebService是一个事实上的标准,其以平台无关性,获得了广泛的应用.本文将讲述如何基于Spring来集成CXF,并开发出第一个Hello World的应用. 1.  Web Service是什么? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web Service技术, 能使