使用Spring + CXF 发布WebService服务

来源:互联网 发布:手机上javaweb编程软件 编辑:程序博客网 时间:2024/05/18 13:27
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。开发WebService的方式有Axis2、JDK、xfire以及CXF,之所以选择CXF是因为它是Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中。

第一步:配置maven依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>demo.ws</groupId>  <artifactId>soap_spring_cxf</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>soap_spring_cxf Maven Webapp</name>  <url>http://maven.apache.org</url>  <properties>    <spring.version>4.0.5.RELEASE</spring.version>    <cxf.version>3.0.0</cxf.version>  </properties>  <dependencies>   <!-- Spring -->    <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>    <!-- CXF -->    <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>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>  </dependencies></project>

第二步:配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"  version="4.0">    <!-- Spring -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 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>/ws/*</url-pattern>    </servlet-mapping></web-app>

第三步:编写WS接口及实现类

接口部分:

package demo.ws.soap_spring_cxf;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface HelloService {    String sayHello(@WebParam(name="name")String name);}

实现部分:

package demo.ws.soap_spring_cxf;import org.springframework.stereotype.Component;@Componentpublic class HelloServiceImpl implements HelloService {    @Override    public String sayHello(String name) {        // TODO Auto-generated method stub        return "Hello "+ name;    }}

注解说明:
@WebService注解的作用是将类暴露为WebService
@WebParam用来定义方法的参数名称

第四步:配置Spring
配置 spring.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://cxf.apache.org/jaxws    http://cxf.apache.org/schemas/jaxws.xsd">    <!--定义 IOC 容器扫描路径-->    <context:component-scan base-package="demo.ws"/>    <!--引入 spring-cxf.xml 文件,用于编写 CXF 相关配置-->    <import resource="spring-cxf.xml"/></beans>

第五步:配置CXF
配置 spring-cxf.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://cxf.apache.org/jaxws    http://cxf.apache.org/schemas/jaxws.xsd">    <jaxws:endpoint id="helloService" implementor="#helloServiceImpl" address="/soap/hello"/></beans>

注意:这里的 implementor 属性值是 #helloServiceImpl,这是 CXF 特有的简写方式,并非是 Spring 的规范,意思是通过 Spring 的 Bean ID 获取 Bean 实例。

第六步:启动Tomcat
将应用部署到 Tomcat 中,在浏览器中输入以下地址可进入 CXF 控制台:

http://localhost:8080/soap_spring_cxf/ws

这里写图片描述

同样也可以通过下面的路径查看WSDL:

http://localhost:8080/soap_spring_cxf/ws/soap/hello?wsdl

发布完毕^_^

0 0
原创粉丝点击