cxf+spring实现webservice

来源:互联网 发布:python客户端开发 编辑:程序博客网 时间:2024/05/19 19:42

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;@WebServicepublic 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.xsdhttp://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、接口发布

原创粉丝点击