WebService的CXF框架与Spring的整合发布服务入门

1、创建WEB项目,将CXF与SPRING的包引入项目(在下载的CXF的lib包里,里面已经整合好了,直接引入进去就行了)
2、创建接口

package com.ckinghan.cxf.server.service;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**
 * 使用CXF框架,在接口上要使用@WebService注解
 * @author Ckinghan
 */
@WebService
//这里指定创建的是SOAP1.2的,如果不指定,创建的是SOAP1.1的,现在大部分使用的都是1.2的
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherService {

    public String getWeather(String cityName);
}

3、创建接口的实现类

package com.ckinghan.cxf.server.service.Impl;

import com.ckinghan.cxf.server.service.WeatherService;

public class WeatherServiceImpl implements WeatherService {

    @Override
    public String getWeather(String cityName) {
        System.out.println("收到来自客户端面的请求:"+cityName);
        return "这是一个好天气";
    }

}

4、在项目中创建config (Source Folder)文件夹,并在此文件中创建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">

                            <!-- spring对Endpoint也做了封装,可以使用jaxws:endpoint进行服务发布 -->
                            <jaxws:endpoint address="/hello" implementor="com.ckinghan.cxf.server.service.test.EndpointTest"/>


    <!-- 配置WebService的发布服务,设置访问地址(默认的已经有地址,只需要将访问的最后路径添加上即可),设置访问接口 -->
    <jaxws:server address="/weather" serviceClass="com.ckinghan.cxf.server.service.WeatherService">
        <!-- 引入接口的实现类 -->
        <jaxws:serviceBean>
            <ref bean="serviceBean" />
        </jaxws:serviceBean>

        <!-- 配置拦截器 -->
        <jaxws:inInterceptors >
            <ref bean="inInterceptor"/>
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <ref bean="outInterceptor"/>
        </jaxws:outInterceptors>

    </jaxws:server>
    <!-- 设置接口的实现类 -->
    <bean name="serviceBean" class="com.ckinghan.cxf.server.service.Impl.WeatherServiceImpl"/>

    <!-- 配置拦截器 -->
    <bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
    <bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>  



</beans>

5、配置web.xml文件,

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>cxfServer1</display-name>

  <!-- 设置Spring的环境 -->
    <context-param>
        <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>/webService/*</url-pattern>
    </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

6、创建测试Sping 封装Endpoint发布服务的文件

package com.ckinghan.cxf.server.service.test;

import javax.jws.WebService;

@WebService
public class EndpointTest {

    public static void main(String[] args) {
        System.out.println("HELLO");

    }

}

7、启动项目

8、通过http://localhost:8080/cxfServer1/webService/weather?wsdl访问项目是否部署成功,如果成功会返回以下界面

这里写图片描述

9、通过http://localhost:8080/cxfServer1/webService/hello?wsdl访问使用发布的服务是否成功,如果成功,会出现 以下界面:
这里写图片描述

10、创建客户端的JAVA项目,并使用 wsdl2java -p com.ckinghan.cxf.client -d 项目目录 http://localhost:8080/cxfServer1/webService/weather?wsdl生成客户端代码
11、引入JAR包
12、创建config Source Folder,并创建applicationContext.xml可以从服务层COPY过来修改

<?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">



    <!-- 配置WebService客户端,设置访问地址(这里需要填写全路径),设置访问接口 ,定义ID用以获取-->
    <jaxws:client id="weatherClient" serviceClass="com.ckinghan.cxf.server.service.WeatherService" address="http://localhost:8080/cxfServer1/webService/weather"/>


</beans>

13、创建测试 类

package com.ckinghan.cxf.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ckinghan.cxf.server.service.WeatherService;


public class CXFClientTest {

    public static void main(String[] args) {
        //获取Spring上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //获取bean对象
        WeatherService weatherService = (WeatherService) context.getBean("weatherClient");
        //调用接口法,获取信息
        String weather = weatherService.getWeather("上海");
        //输出获取的信息
        System.out.println(weather);
    }

}

14、执行。输出结果如下:
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值