cxf+spring实现webservice

6 篇文章 1 订阅

1、构建maven项目,工程结构如下:

这里需要特别指出就是cxf-core-3.1.12.jar类路径META-INF/cxf下有一个cxf.xml的配置文件,这个在applicationContext.xml配置文件中会使用到。

2、编写pom.xml文件,这里需要引入cxf-rt-frontend-jaxws,cxf-rt-transports-http两个jar,因为要和spring整合,这里还需要引入spring的依赖。下面的pom文件已经是最精简的。

<properties>
          <cxf.version>3.1.12</cxf.version>
          <spring.version>4.2.0.RELEASE</spring.version>
  </properties>
  <dependencies>
          <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-rt-frontend-jaxws</artifactId>
                <version>${cxf.version}</version>
          </dependency>
          <dependency>
                 <groupId>org.apache.cxf</groupId>
                 <artifactId>cxf-rt-transports-http</artifactId>
                 <version>${cxf.version}</version>
          </dependency>      
          <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-beans</artifactId>
                  <version>${spring.version}</version>
          </dependency>
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
          </dependency>
          <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${spring.version}</version>
          </dependency>
  </dependencies>

3、接口和实现类:自定义一个webservice接口BaseService并开启注解,自定义BusinessService类实现BaseService接口。

BaseService.java

package com.xxx.service;

import javax.jws.WebService;

@WebService
public interface BaseService {
    public int add(int a,int b);
    public String sayHello(String name);
}

BusinessService.java

package com.xxx.service.impl;

import javax.jws.WebService;

import com.xxx.service.BaseService;
@WebService(endpointInterface="com.xxx.service.BaseService")
public class BusinessService implements BaseService {

    @Override
    public int add(int a, int b) {
	return a+b;
    }

    @Override
    public String sayHello(String name) {
	return "hello,"+name;
    }

}

4、web.xml配置文件

<?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>webservice</display-name>
    <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>
     <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>/webservice/*</url-pattern>
      </servlet-mapping>
</web-app>

5、applicationContext.xml,这里需要指出的是在创建applicationContext.xml之后,在命名空间中引入jaxws,文件中需要引入一个cxf.xml文件,这个文件无需手动创建,他在cxf-core-3.1.12.jar类路径下的META-INF/cfx下。在第一步的项目结构中可以看到。

<?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://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd
		http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

     <!-- 该文件是在cxf-core-3.1.12.jar的类路径下的META-INF/cxf/cxf.xml -->
     <import resource="classpath:META-INF/cxf/cxf.xml"/>
      <bean id="business" class="com.xxx.service.impl.BusinessService"></bean>
      <jaxws:endpoint id="businessService" address="/business" implementor="#business"></jaxws:endpoint>
</beans>

6、接口发布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值