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` --数 ...
随机推荐
- CentOS 6.5 升级内核到 3.10.28
本文适用于CentOS 6.4, CentOS 6.5,亲测可行,估计也适用于其他Linux发行版. 1. 准备工作 1.1 下载源码包 Linux内核版本有两种:稳定版和开发版 ,Linux内核版本 ...
- operator[],识别读操作和写操作
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 虚拟机下Redhat9 网络配置问题(转)
原文链接:http://www.programgo.com/article/38031929690/ edhat 9/redhat as 3装在虚拟机vmware上之后,连接网络是出现问题 Deter ...
- Elasticsearch 在分布式系统中深度分页问题
理解为什么深度分页是有问题的,我们可以假设在一个有 5 个主分片的索引中搜索. 当我们请求结果的第一页(结果从 1 到 10 ),每一个分片产生前 10 的结果,并且返回给 协调节点 ,协调节点对 5 ...
- 报错:Type mismatch: cannot convert from Object to Car
问题描述: 一个非常简单的spring项目,用静态工厂方法配置bean实例.项目的目录结构如下: 代码如下: Car.java package com.tt.spring.beans.factory; ...
- An error report file with more information is saved as hs_err_pid2756.log
An error report file with more information is saved as hs_err_pid2756.log weblogic启服务时遇到的问题,重新部署都不行, ...
- ISO 18000-6c 访问标签--应用程序访问操作ISO 18000-6C标签的方法
标签访问操作包含三部分: 1.标签选择.某些场景,应用程序只对特定的标签进行操作,Indy Host Library可以指定标签选择条件. 2.执行盘存操作.上报单化后标签EPC区的数据. 3.标签单 ...
- HAWQ + MADlib 玩转数据挖掘之(一)——安装
一.MADlib简介 MADlib是Pivotal公司与伯克利大学合作的一个开源机器学习库,提供了精确的数据并行实现.统计和机器学习方法对结构化和非结构化数据进行分析,主要目的是扩展数据库的分析能力, ...
- wampserver搭建本地服务器
打开..\wamp\bin\apache\apache2.4.9\conf\httpd.conf配置文件, <Directory "c:/wamp/www/"> # # ...
- Mac安装最新tensorflow遇到的坑,记录下方便后人
之前其他mac电脑安装tensorflow时候一切顺利,一行命令sudo pip install tensorflow就高搞定了,但是今天在新mac上安装tensorflow时候出现了一个bug,搞了 ...