webservice之cxf实现[web项目中基于maven与spring整合]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeonWang_Fly/article/details/50633551

webservice现开发的已实现组件不少,使用过的就有xfire及cxf.
cxc基于maven与spring整合过程如下:

STEP 1. 依赖包添加

在pom.xml文件的标签中增加如下(版本号依个人需要调整):

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf</artifactId>
        <version>2.7.7</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>2.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>2.7.7</version>
    </dependency>

STEP 2. 编写服务类代码

接口定义:

package test.service;

import javax.jws.WebService;

/**
 * 
 * @Description: webservice测试服务
 * @author LeonWang
 * @date 2016-2-2 下午2:46:33 
 *
 */
@WebService
public interface MyService {
    public void sayHi(String name);
}

接口实现

package test.service.impl;

import javax.jws.WebService;

import test.service.MyService;
@WebService(endpointInterface = "test.service.MyService")
public class MyServiceImpl implements MyService {
    @Override
    public void sayHi(String name) {
        System.out.println("hello,"+name);
    }

}

STEP 3.配置springbean及webservice发布

创建cxf-servlet.xml将其放在spring配置文件扫描路径下

<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-servlet.xml"/>
    <bean id="myService" class="test.service.impl.MyServiceImpl" />
    <jaxws:endpoint id="testService" implementor="#myService" address="/testService"/>
</beans>

说明:
1. cxf-servlet.xml中import导入的文件不用自己创建,这是在依赖包中的。
2. webservice发布配置中implementor可以直接吸入实现类,如:

<jaxws:endpoint id="testService" implementor="test.service.impl.MyServiceImpl" address="/testService"/>

3.address参数是重点,这是webservice发布后其wsdl的相对路径,其绝对路径为应用访问路径/cxf拦截路径/address?wsdl

STEP 4.配置webservice拦截

在web.xml中增加cxf的拦截配置,如下:

<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>/services/*</url-pattern>
</servlet-mapping>

STEP 5.测试服务是否发布成功

部署并启动服务,访问wsdl,本配置访问如下:
http://localhost:8001/webserviceTest/services/testService?wsdl
我的项目部署在本地,端口8001,应用访问名称为webserviceTest,cxf拦截配置为/services/*,发布相对地址为/testService.

猜你喜欢

转载自blog.csdn.net/LeonWang_Fly/article/details/50633551