IIdea使用CXF开发WebService
写这篇文章主要是用于增强记忆,而我参考的是这位朋友的随笔,链接如下
http://www.xiaomager.com/415.html
服务端开发过程
1.首先创建一个maven项目,如下图

2.添加项目的依赖包以及设置相关配置
提示:首先介绍一下基础环境 ,开发编译器 intellij idea ,我们的jdk是1.7,tomcat是7,spring使用的是spring4,cxf准备使用3.1.4,这里特别需要说明的是,cxf 3.0以后的版本只能在jdk1.7上使用,如果在1.6使用的话,会直接报错的。
<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.rainsoft</groupId>
<artifactId>cxfWebservice</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version> <name>cxfWebservice</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<!-- spring版本号 -->
<spring.version>4.3.2.RELEASE</spring.version>
<!-- CXF版本号 -->
<cxf.version>3.1.4</cxf.version>
</properties> <dependencies>
<!-- spring核心包 -->
<!-- springframe start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</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-core</artifactId>
<version>${spring.version}</version>
</dependency> <!-- CXF webservice -->
<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.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency> </dependencies>
<build>
<finalName>cxfWebservice</finalName>
<defaultGoal>compile</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
<excludes>
<!--测试环境-->
<exclude>conf/test/*</exclude>
<!--生产环境-->
<exclude>conf/production/*</exclude>
<!--开发环境-->
<exclude>conf/development/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/conf/${profiles.active}</directory>
<targetPath>conf</targetPath>
</resource>
</resources>
<plugins>
<!-- compiler插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<showWarnings>true</showWarnings>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<!-- 解决maven命令console出现中文乱码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins> </build> <distributionManagement>
<repository>
<id>nexus-releases</id>
<url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url>
</snapshotRepository>
</distributionManagement> <profiles>
<!--生产环境-->
<profile>
<id>production</id>
<properties>
<profiles.active>production</profiles.active>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile> <profile>
<id>development</id>
<properties>
<profiles.active>development</profiles.active>
</properties>
<activation><activeByDefault>true</activeByDefault></activation>
</profile>
</profiles>
</project>
保存后,需要执行maven 的 clean, install 来下载jar包到maven的本地库。
3.添加配置文件支持
一共有三处配置文件需要更新
第一处:需要新增一个cxf的配置文件,这里取名为cxf-servlet.xml,内容如下:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <context:component-scan base-package="com.daisy" />
<bean id="cxfdemo" class="com.daisy.cxf.server.impl.MyCxfServerImpl">
</bean>
<jaxws:endpoint id="cxfService" implementor="#cxfdemo" address="/cxfserver" />
</beans>
此处需要根据自己的包以及配置文件,添加配置
这里面主要就是定义了一个cxfService,它的 实现类是com.daisy.cxf.server.impl.CxfServerImpl,这个 实现类我们在第三步骤来加上,其次还定义了一个/cxfserver的路径,即我们的cxfserver服务端的请求路径。
第二处:需要在spring主配置applicationContext文件里把这个新建的文件添加上,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:annotation-config /> <import resource="cxf-servlet.xml" /> </beans>
第三处:就是需要在web.xml里面配置cxf的servlet,具体如下:
该处定义了请求webService的前缀,及通过引入一个org.apache.cxf.transport.servlet.CXFServlet的servlet来处理 所有前缀 /webService/*的请求,所以 我们的cxf的全路径应该是 这里的servlet-mapping加上第一处的address,即 /webService/cxfserver。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>cxfWebservice</display-name> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. 添加cxf服务端的接口类和接口实现类,目录结构如下

java实现代码为 IMyCxfServer.java
此处必须声明一下,在接口的上面也必须加上 @WebService , 我开始抱着试试的心理将它去掉了,结果在客户端调用的时候就怎么都找不到对应的方法了
package com.daisy.cxf.server; import javax.jws.WebService; /**
* @author daisy
* @description IMyCxfServer
* @date 2017/4/8
*/
@WebService
public interface IMyCxfServer {
String sayHello(String name);
}
MyCxfServerImpl.java
package com.daisy.cxf.server.impl; import com.daisy.cxf.server.IMyCxfServer; import javax.jws.WebService; /**
* @author daisy
* @description MyCxfServerImpl
* @date 2017/4/8
*/
@WebService
public class MyCxfServerImpl implements IMyCxfServer {
@Override
public String sayHello(String name) {
return "hello "+name;
}
}
以上代码表示的意思很明白,即服务端提供一个sayHello的方法,将客户端传递的字符串参数 前面加上 hello 后返回。
5. 添加cxf服务端的接口类和接口实现类,目录结构如下
将项目添加到tomcat服务器上运行,访问链接 http://localhost:9090/daisyCxf//webService/cxfserver?wsdl 即可看到结果,如下图所示

IIdea使用CXF开发WebService的更多相关文章
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 使用cxf开发webservice应用时抛出异常
在使用cxf开发webservice应用时,报出了类似下面的错误 JAXB: [javax.xml.bind.UnmarshalException: unexpected element (uri:& ...
- 【WebService】使用CXF开发WebService(四)
CXF简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix ...
- 使用cxf开发webservice接口
项目中经常用到开发webservice接口,及调用webService接口.这里讲解如何使用cxf开发webService接口. 一.webservice介绍及理解 webservice是一种跨平台, ...
- 3.使用CXF开发webService
CXF 简介 关于 Apache CXF Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- (三)使用CXF开发WebService客户端
前面一讲开发了webservice服务器端接口,今天的话,我们来开发webservice客户端,让大家来体验下过程: 首先建一个Maven项目,项目名字,WS_Client: 然后我们要用CXF给我们 ...
- CXF开发WebService
CXF开发Web Service 参考链接 使用 spring 框架来集成 Web Services 开发 浏览器调用接口 大概这样, 没成功 加@WebMethod(action="get ...
随机推荐
- 借助rownum中求Oracle表中前三名(三甲:状元榜眼探花)的方法(总计三种方法,以讲述rownum的使用为主)
要求前三名,MySQL中有order by排序,limit限制数量,结果很容易得到,而且limit的执行顺序也在order by之后,写出的sql高效易懂而不易出错. 但在oracle中,由于没有li ...
- 发布jar包到服务器读取resource目录下文件
* 解决:当项目打包成jar之后resources路径下面的证书文件访问不到* 思路:* 1.运行时先复制一个jar* 2.将复制的jar解压到jar文件目录* 3.删除复制的jar跟解压的非证书文件 ...
- Python中自己不熟悉的知识点记录
重点笔记: Python 它是动态语言 动态语言的定义:动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用.它是一类 在 运行时可以改变其结构的语言 : ...
- JS基础回顾_函数
函数 不要使用C风格的大括号 // log function return1() { return { name: 'oceans', } } function return2() { return ...
- Linux系统环境基于Docker搭建Mysql数据库服务实战
开放端口规划: mysql-develop:3407 mysql-test: 3408 mysql-release: 3409 ps: 1.不推荐使用默认端口-3306,建议自定义端口 2.如果采用阿 ...
- python中使用token模拟登录
背景:在接口测试中我们经常是需要一个登陆token,或者获取其他用到的参数来关联下一个接口用到的参数. Token的意义及用法 一.Token的来源: 当客户端多次向服务端请求数据时,服务端就需要多次 ...
- python操作从数据库中获取数据的接口
1.输入一个表名,获取表里面的数据 2.判断用户是否存在,如果不存在就添加到数据库里面
- Linux常用的三种软件安装方式
一:Linux源码安装 1.解压源码包文件 源码包通常会使用tar工具归档然后使用gunzip或bzip2进行压缩,后缀格式会分别为.tar.gz与.tar.bz2,分别的解压方式: ...
- command三国杀开发日记20200914
目前状态 一时脑热开始写的东西,计划完全使用C语言实现,尽量使用通用接口,能够在windows上直接运行 几乎是一穷二白,初步搭建了牌堆.玩家信息接口体,编写了简单的UI函数,能够将玩家信息显示在屏幕 ...
- 【运维】Vmware虚拟机静态IP的设置
这几天学习大数据,搭建的集群服务器由于Vmware内部实现的虚拟网关,动态分配ip,使得每次ip更改后,均需要修改集群节点的每个hosts文件,不然集群间联系会出错,因此为了杜绝这个问题,这里修改集群 ...