Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

  先贴出maven项目的pom.xml,这里使用了spring,而且有坑,spring的版本选用太新会启动不了服务器,后改为3.X版本才好。

  

 <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>com.bigbang</groupId>
<artifactId>cxf</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cxf Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<cxf.version>3.1.11</cxf.version>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<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>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>cxf</finalName>
</build> </project>

  项目web.xml文件的配置如下:

  

  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/application*.xml</param-value>
</context-param>
<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>/service/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  其中CXFServlet是要访问的webservice入口配置。

  Spring文件的相关配置如下:

  

 <?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://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="loginService" class="com.bigbang.cxf.service.LoginServiceImpl"/>
<jaxws:endpoint implementor="#loginService" address="/login"/>
</beans>

  需要引入jax-ws的命名空间。并且引入两个xml配置文件,这两个文件在cxf的包里面,可以不用关心。 此处配置了一个bean,该bean是实现webservice服务的实现类。里面是具体的业务逻辑。

  jaxws:endpoint是配置webservice端地址的,implementor指向一个实现类,address是webservice的访问地址。结合web.xml里面的cxfServlet配置,访问webservice的地址应该为:http://IP:端口/项目名/service/login?wsdl

  webservice的接口和实现类如下:

  接口类:

  

 package com.bigbang.cxf.service;

 import javax.jws.WebParam;
import javax.jws.WebService; /**
* @author pzj
*
*/
@WebService
public interface LoginService { String login(@WebParam(name="name")String name,@WebParam(name="password")String password);
}

  @webservice注解说明这是一个webservice类,@WebParam是指明webservice接口方法的参数名称。类似于springmvc的@requestParam注解。此处如果不指明参数名称,webservice的wsdl文件里将使用arg0,arg1...代替,在写客户端代码请求webservice的时候,将引起困扰,不易编写代码。  

  实现类为:

  

 package com.bigbang.cxf.service;

 import javax.jws.WebService;

 /**
* @author pzj 2017年5月16日 下午4:58:43
*
*/
@WebService(endpointInterface="com.bigbang.cxf.service.LoginService")
public class LoginServiceImpl implements LoginService { /* (non-Javadoc)
* @see com.bigbang.cxf.LoginService#login(java.lang.String, java.lang.String)
*/
@Override
public String login(String name, String password) {
return name+"登录成功!密码是:"+password;
} }

  和上面接口不同的是,此处需要指明终端接口类的地址(包名+类名)。

  全部准备好之后,使用jetty启动,会显示如下启动信息:

  

五月 18, 2017 9:58:05 上午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.bigbang.com/}LoginServiceImplService from class com.bigbang.cxf.service.LoginService
五月 18, 2017 9:58:06 上午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /login
2017-05-18 09:58:06.173:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080

  说明webservice发布成功了,访问http://localhost:8080/service/login?wsdl,会生成xml页面:

  

 This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.cxf.bigbang.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="LoginServiceImplService" targetNamespace="http://service.cxf.bigbang.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.cxf.bigbang.com/" elementFormDefault="unqualified" targetNamespace="http://service.cxf.bigbang.com/" version="1.0">
<xs:element name="login" type="tns:login"/>
<xs:element name="loginResponse" type="tns:loginResponse"/>
<xs:complexType name="login">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
<xs:element minOccurs="0" name="password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="loginResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="login">
<wsdl:part element="tns:login" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="loginResponse">
<wsdl:part element="tns:loginResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="LoginService">
<wsdl:operation name="login">
<wsdl:input message="tns:login" name="login"></wsdl:input>
<wsdl:output message="tns:loginResponse" name="loginResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LoginServiceImplServiceSoapBinding" type="tns:LoginService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="login">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="login">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="loginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LoginServiceImplService">
<wsdl:port binding="tns:LoginServiceImplServiceSoapBinding" name="LoginServiceImplPort">
<soap:address location="http://localhost:8080/service/login"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

  

  

CXF+Spring搭建webservice服务的更多相关文章

  1. 使用CXF和spring搭建webService服务

    虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验 ...

  2. 使用CXF+Spring发布WebService,启动报错

    使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.util.digester.SetProper ...

  3. Spring Boot+CXF搭建WebService服务参考资料

    pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...

  4. Spring整合CXF之发布WebService服务

    今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...

  5. 通过CXF方式实现webservice服务

    一.CXF的介绍 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 S ...

  6. 使用xfire搭建webService服务

    后边有个项目需要接入4A,要用到webService服务,暂时还不确定是不是会有我的事,但为了有备无患,还是抽时间学习了以下相关的知识. 本来我所了解到的发布webService服务有用cxf和xfi ...

  7. 使用cxf3.0.4搭建webservice服务需要的最精简jar包

    转自:https://blog.csdn.net/w1014074794/article/details/47862163 下面是测试结果,只列出报错了的测试: 1.org.apache.catali ...

  8. cxf+spring发布webservice和调用

    maven项目配置http://cxf.apache.org/docs/using-cxf-with-maven.html <properties> <cxf.version> ...

  9. ibatis+spring+cxf+mysql搭建webservice

    首先需必备:mysql.myeclipse6.5.apache-cxf-2.6.2 一.建数据库,库名:cxf_demo:表名:book CREATE DATABASE `cxf_demo`  --数 ...

随机推荐

  1. NPOI:初次操作(新建Excel)

    1. 由于在某些电脑上没有安装office,或者有权限限制,使用COM组件进行读写Excel的话会出现问题, 为此,NPOI是一个很好的选择,NPOI可以在上述环境中满足Office的操作需求,并且功 ...

  2. MySQL使用RPM包方式安装

        CentOS7安装MySQL的方法之RPM包方式        

  3. querySelectorAll 与jquery.find 与htmlcollection 的区别

    querySelector 和 querySelectorAll 规范定义 querySelector 和 querySelectorAll 方法是 W3C Selectors API Level 1 ...

  4. LINUX中的DNS服务---DNS正向、反向和双向解析

    一.DNS的正向解析 也就是域名解析为IP地址进行访问! 1)vim  /etc/named.conf   ---->  删除forwarders所在行 2)vim  /etc/named.rf ...

  5. Function.bind 方法

    this.num = 9; var mymodule = { num: 81, getNum: function() { return this.num; } }; module.getNum(); ...

  6. Linux:ln命令详解(软连接,硬链接)

    ln ln命令用来为文件创件连接,连接类型分为硬连接和软连接(符号连接)两种,默认的连接类型是硬连接.如果要创建软连接必须使用"-s"选项. 硬链接 建立硬链接时,在另外的目录或本 ...

  7. three.js入门系列之视角和辅助线

    假设你已经创建好了three.js的开发环境(我是写在vue项目中的),那么接下来,从头开始演示是如何用three.js来构建3D图形的.(笔记本写的代码,屏幕小,所以为了能够整屏看到完整代码,就将字 ...

  8. poscms基于list标签实现的查询分页功能

    poscms系统本身有一个在查询页(search页面)实现的查询分页功能,基于系统封装的php函数dr_search_url() 但是今天的需求除了导航栏.列表页.详情页都实现查询功能外,关键是有两个 ...

  9. ftp上传下载记录

    1,准备ftp环境 下载最新的ftp客户端:https://filezilla-project.org/ftp/001.png,选择linux下面的版本,如002.png所示: 在window10下面 ...

  10. React-Native基础_5.列表视图ListView 网络数据展示

    //获取网络数据 并用列表展示 豆瓣Top250 api /** * Sample React Native App * https://github.com/facebook/react-nativ ...