废话少说,先在Eclipse中新建一个Java Project
(可以不是WTP的Dynamic Web Project)
选择Java Project
再看pom.xml
我们使用cxf 3.1.4版本,
<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.cnblog.richaaaard.cxfstudy</groupId> <artifactId>cxf-test-standalone-ws-helloworld</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>cxf-test-standalone-ws-helloworld Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- <cxf.version>2.7.18</cxf.version> --> <!-- 注意使用2.x.x版本的同学,下面例子中引入cxf-bundle-jaxrs是不必要的,因为jaxws启动一个服务需要类JaxWsServerFactoryBean在cxf-rt-frontend-jaxws.jar中-->
<cxf.version>3.1.4</cxf.version> </properties> <dependencies> <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-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>${cxf.version}</version> </dependency> <!-- <dependency> --> <!-- <groupId>org.apache.cxf</groupId> --> <!-- <artifactId>cxf-bundle-jaxrs</artifactId> --> <!-- <version>${cxf.version}</version> --> <!-- </dependency> --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>cxfstudy</finalName> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <configuration> <contextPath>/</contextPath> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>9000</port> </connector> </connectors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
另外有例子给出的slf4j是slf4j-jdk14 (http://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14)的版本,如果用jdk5以上的同学可以将其替换成slf4j-simple实现。
CXF Web Service (Annotation)实现,上代码
CXF支持两种方式发布一个web service,一种是Java Annotation(Bottom up),另外一种是先定义好wsdl和xsd schema然后通过工具生成(Top down),我们这里的例子先介绍Java Annotation。
首先我们需要定义一个web service接口类HelloWorld,并加上Annotation
package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService public interface HelloWorld { @WebMethod @WebResult String sayHi(@WebParam String text); }
在此我们先不介绍 @WebService、 @WebMethod、@WebResult还有@WebParam如何工作的。
然后我们需要用HelloWorldImpl来实现这个接口类,
package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services; public class HelloWorldImpl implements HelloWorld { public String sayHi(String name) { String msg = "Hello " + name + "!"; return msg; } }
发布一个Web Service
我们先用一个最简单的方式,使用javax.xml.ws.Endpoint来发布
package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server; import javax.xml.ws.Endpoint; import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl; public class SimpleServer { public static void main(String[] args) throws Exception { System.out.println("Starting Server"); HelloWorldImpl implementor = new HelloWorldImpl(); String address = "http://localhost:9000/ws/HelloWorld"; Endpoint.publish(address, implementor); } }
注意:这里的address路径是大小写敏感的,如果发布成"http://localhost:9000/ws/ helloWorld ",而客户端使用/HelloWorld会找不到服务。
直接Run As..-> Java Application
我们可以在Eclipse中看到一个jetty服务正常运行
Starting Server 128 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldImplService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld 487 [main] INFO org.apache.cxf.endpoint.ServerImpl - Setting the server's publish address to be http://localhost:9000/ws/HelloWorld 506 [main] INFO org.eclipse.jetty.server.Server - jetty-8.1.15.v20140411 554 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started SelectChannelConnector@localhost:9000
此时,我们通过浏览器访问http://localhost:9000/ws/HelloWorld?WSDL
如何访问我们发布的这个服务呢?
我们利用jaxws中的JaxWsProxyFactoryBean来消费这个服务
package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld; public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:9000/ws/HelloWorld"); HelloWorld helloworld = (HelloWorld) factory.create(); String reply = helloworld.sayHi("HI"); System.out.println("Server said: " + reply); System.exit(0); } }
同样右键选择Client.java,Run As.. ->Java Application,我们可以看到运行后台执行的日志最后成功返回“Server said: Hello HI!”
114 [main] INFO org.apache.cxf.service.factory.ReflectionServiceFactoryBean - Creating Service {http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/}HelloWorldService from class com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld 531 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Outbound Message --------------------------- ID: 1 Address: http://localhost:9000/ws/HelloWorld Encoding: UTF-8 Http-Method: POST Content-Type: text/xml Headers: {Accept=[*/*], SOAPAction=[""]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHi xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><arg0>HI</arg0></ns2:sayHi></soap:Body></soap:Envelope> -------------------------------------- 748 [main] INFO org.apache.cxf.services.HelloWorldService.HelloWorldPort.HelloWorld - Inbound Message ---------------------------- ID: 1 Response-Code: 200 Encoding: UTF-8 Content-Type: text/xml;charset=UTF-8 Headers: {Content-Length=[258], content-type=[text/xml;charset=UTF-8], Server=[Jetty(8.1.15.v20140411)]} Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHiResponse xmlns:ns2="http://services.helloworld.ws.standalone.cxftest.richaaaard.cnblog.com/"><return>Hello HI!</return></ns2:sayHiResponse></soap:Body></soap:Envelope> -------------------------------------- Server said: Hello HI!
上面Outbound Message和Inbound Message是我们加的两个Logging Interceptor输出的日志,关于Interceptor的原理和更多使用方式,会专题介绍。
这样就完成了一个Web Service的发布和消费
*服务端的扩展
除了用一个最简单的javax.xml.ws.Endpoint来发布CXF web service,我们还可以使用cxf-rt-frontend-jaxws.jar中的JaxWsServerFactoryBean来发布一个web service,它可以让我们更多的控制web service的行为,比如说加Logging Interceptor之类
package com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.server; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorld; import com.cnblog.richaaaard.cxftest.standalone.ws.helloworld.services.HelloWorldImpl; public class Server { public static void main(String[] args) throws Exception { JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(HelloWorld.class); HelloWorldImpl implementor = new HelloWorldImpl(); factory.setServiceBean(implementor); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setAddress("http://localhost:9000/ws/HelloWorld"); factory.create(); System.out.println("Server start..."); Thread.sleep(60 * 1000); System.out.println("Server exit..."); System.exit(0); } }
参考:
http://blog.csdn.net/kongxx/article/details/7525476
https://cwiki.apache.org/confluence/display/CXF20DOC/A+simple+JAX-WS+service
相关推荐
1. AXIS2:Apache提供的重量级Web Service框架,能够生成多种语言的客户端代码,适合开发跨语言的Web Service。然而,其复杂性较高,依赖众多JAR包,部署和集成可能较为困难。 2. CXF:源自XFire的SOA框架,易于...
首先,CXF是一个由Apache基金会维护的重量级SOA(面向服务架构)框架,它支持ESB(企业服务总线),并实现了Web服务、SOAP和WSDL的功能。CXF源自XFire项目,但在功能和性能上进行了优化,因此比XFire更受欢迎。CXF的...
XFire提供了一种简单的方式来创建和消费Web服务,支持JAX-WS标准。通过XFire,开发者可以轻松地将Java方法暴露为Web服务,或者调用其他Web服务。虽然现在CXF更为流行,但理解XFire的基本原理有助于理解现代Web服务的...
2. **轻量级框架**: 相比于其他重量级的Java EE容器,XFire的设计理念是轻便和快速,它不依赖于大型的应用服务器,可以在简单的Java运行环境中运行,降低了对硬件资源的需求。 3. **CXF整合**: 在XFire 1.2.6之后,...
内容概要:本文档《数据结构》(02331)第一章主要介绍数据结构的基础概念,涵盖数据与数据元素的定义及其特性,详细阐述了数据结构的三大要素:逻辑结构、存储结构和数据运算。逻辑结构分为线性结构(如线性表、栈、队列)、树形结构(涉及根节点、父节点、子节点等术语)和其他结构。存储结构对比了顺序存储和链式存储的特点,包括访问方式、插入删除操作的时间复杂度以及空间分配方式,并介绍了索引存储和散列存储的概念。最后讲解了抽象数据类型(ADT)的定义及其组成部分,并探讨了算法分析中的时间复杂度计算方法。 适合人群:计算机相关专业学生或初学者,对数据结构有一定兴趣并希望系统学习其基础知识的人群。 使用场景及目标:①理解数据结构的基本概念,掌握逻辑结构和存储结构的区别与联系;②熟悉不同存储方式的特点及应用场景;③学会分析简单算法的时间复杂度,为后续深入学习打下坚实基础。 阅读建议:本章节内容较为理论化,建议结合实际案例进行理解,尤其是对于逻辑结构和存储结构的理解要深入到具体的应用场景中,同时可以尝试编写一些简单的程序来加深对抽象数据类型的认识。
内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
某型自动垂直提升仓储系统方案论证及关键零部件的设计.zip
2135D3F1EFA99CB590678658F575DB23.pdf#page=1&view=fitH
可以搜索文本内的内容,指定目录,指定文件格式,匹配大小写等
Windows 平台 Android Studio 下载与安装指南.zip
Android Studio Meerkat 2024.3.1 Patch 1(android-studio-2024.3.1.14-windows.zip)适用于Windows系统,文件使用360压缩软件分割成两个压缩包,必须一起下载使用: part1: https://download.csdn.net/download/weixin_43800734/90557033 part2: https://download.csdn.net/download/weixin_43800734/90557035
国网台区终端最新规范
国网台区终端最新规范
1.【锂电池剩余寿命预测】Transformer-GRU锂电池剩余寿命预测(Matlab完整源码和数据) 2.数据集:NASA数据集,已经处理好,B0005电池训练、B0006测试; 3.环境准备:Matlab2023b,可读性强; 4.模型描述:Transformer-GRU在各种各样的问题上表现非常出色,现在被广泛使用。 5.领域描述:近年来,随着锂离子电池的能量密度、功率密度逐渐提升,其安全性能与剩余使用寿命预测变得愈发重要。本代码实现了Transformer-GRU在该领域的应用。 6.作者介绍:机器学习之心,博客专家认证,机器学习领域创作者,2023博客之星TOP50,主做机器学习和深度学习时序、回归、分类、聚类和降维等程序设计和案例分析,文章底部有博主联系方式。从事Matlab、Python算法仿真工作8年,更多仿真源码、数据集定制私信。
Android项目原生java语言课程设计,包含LW+ppt
大学生入门前端-五子棋vue项目
这是一个完整的端到端解决方案,用于分析和预测阿联酋(UAE)地区的二手车价格。数据集包含 10,000 条二手车信息,覆盖了迪拜、阿布扎比和沙迦等城市,并提供了精确的地理位置数据。此外,项目还包括一个基于 Dash 构建的 Web 应用程序代码和一个训练好的 XGBoost 模型,帮助用户探索区域市场趋势、预测车价以及可视化地理空间洞察。 数据集内容 项目文件以压缩 ZIP 归档形式提供,包含以下内容: 数据文件: data/uae_used_cars_10k.csv:包含 10,000 条二手车记录的数据集,涵盖车辆品牌、型号、年份、里程数、发动机缸数、价格、变速箱类型、燃料类型、颜色、描述以及销售地点(如迪拜、阿布扎比、沙迦)。 模型文件: models/stacking_model.pkl:训练好的 XGBoost 模型,用于预测二手车价格。 models/scaler.pkl:用于数据预处理的缩放器。 models.py:模型相关功能的实现。 train_model.py:训练模型的脚本。 Web 应用程序文件: app.py:Dash 应用程序的主文件。 callback
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
资源内项目源码是来自个人的毕业设计,代码都测试ok,包含源码、数据集、可视化页面和部署说明,可产生核心指标曲线图、混淆矩阵、F1分数曲线、精确率-召回率曲线、验证集预测结果、标签分布图。都是运行成功后才上传资源,毕设答辩评审绝对信服的保底85分以上,放心下载使用,拿来就能用。包含源码、数据集、可视化页面和部署说明一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.txt文件,仅供学习参考, 切勿用于商业用途。
此为代码审查工具 可查 文件数,字节数,总行数,代码行数,注释行数,空白行数,注释率等