CXF+Spring搭建webservice服务
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服务的更多相关文章
- 使用CXF和spring搭建webService服务
虽然下一个项目需要使用xfire,但是在查资料的过程中还是看到有不少地方都说cxf比xfire更好,cxf继承了xfire,但是不仅仅包含xfire,因此便也一起来尝试尝试.大概是有了xfire的经验 ...
- 使用CXF+Spring发布WebService,启动报错
使用CXF+Spring发布WebService,启动报错,日志如下: 五月 12, 2017 9:01:37 下午 org.apache.tomcat.util.digester.SetProper ...
- Spring Boot+CXF搭建WebService服务参考资料
pom.xml文件引入包: <!--WerbService CXF依赖--> <dependency> <groupId>org.apache.cxf</gr ...
- Spring整合CXF之发布WebService服务
今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...
- 通过CXF方式实现webservice服务
一.CXF的介绍 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 S ...
- 使用xfire搭建webService服务
后边有个项目需要接入4A,要用到webService服务,暂时还不确定是不是会有我的事,但为了有备无患,还是抽时间学习了以下相关的知识. 本来我所了解到的发布webService服务有用cxf和xfi ...
- 使用cxf3.0.4搭建webservice服务需要的最精简jar包
转自:https://blog.csdn.net/w1014074794/article/details/47862163 下面是测试结果,只列出报错了的测试: 1.org.apache.catali ...
- cxf+spring发布webservice和调用
maven项目配置http://cxf.apache.org/docs/using-cxf-with-maven.html <properties> <cxf.version> ...
- ibatis+spring+cxf+mysql搭建webservice
首先需必备:mysql.myeclipse6.5.apache-cxf-2.6.2 一.建数据库,库名:cxf_demo:表名:book CREATE DATABASE `cxf_demo` --数 ...
随机推荐
- 添加resx文件过程笔记
为了测试资源文件,今天上午耗掉了.终于实现了,原来是要按照它自动建文件夹,不可以手动建.下图 见其他语言的时候一定要:Mytest.zh-CN.resx 这样,加上后缀名字 代码中获取资源 http: ...
- LeetCode OJ:Valid Sudoku(有效数独问题)
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- (转)Android学习笔记②——HelloWorld的创建已经基本知识
开发第一应用 可以开发属于自己的应用,是否有点小激动?好吧!让我们开始,首先点击Start a new Android Studio Project创建工程:接下来需要输入应用名称(第一个字母要大写) ...
- 【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- php自动添加相关文章
{pc:content action="relation" relation="$relation" id="$id" catid=&quo ...
- CSS琐碎[1]
(1)letter-spacing 设置字符间局,用长度指定(百分比兼容性不好) 没有间距 间距6px API:http://gucong3000.github.io/css-book/propert ...
- webdriver常用函数总结
#1 创建浏览器对象 driver = webdriver.Chrome() #2 设置隐式等待10秒 driver.implicitly_wait(10) #3 最大化浏览器窗口 driver.ma ...
- 在制作跨平台的 NuGet 工具包时,如何将工具(exe/dll)的所有依赖一并放入包中
NuGet 提供了工具类型的包支持,生成一个基于 .NET Core 的 dll 或者基于 .NET Framework 的 exe 之后,你几乎可以对项目做任何事情.但是,默认情况下,NuGet 不 ...
- C#/.NET 中推荐的 Dispose 模式的实现
如果你觉得你的类需要实现 IDisposable 接口,还是需要注意一些坑的.不过前人准备了 Dispose 模式 供我们参考,最大程度避免这样的坑. C#程序中的 Dispose 方法,一旦被调用了 ...
- Quartz 2D编程指南(4) - 颜色和颜色空间
不同的设备(显示器.打印机.扫描仪.摄像头)处理颜色的方式是不同的.每种设备都有其所能支持的颜色值范围.一种设备能支持的颜色可能在其它设备中无法支持.为了有效的使用颜色及理解Quartz 2D中用于颜 ...