cxf-spring-pratice-service
其实吧,这篇博客基本上就是照搬了官方的How-to.
2017/11/15 时隔六个月, 重新下组织格式和内容.
诸如webservice,WSDL的定义及作用,还有意义啥的咱就不再废话了,直接上干货。
1. 引入cxf相关的依赖
1.1 使用maven
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.1</version>
</dependency>
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.1</version>
</dependency>
1.2 直接引入相关jar
真心不建议采用这种方式!
asm-5.0.4.jar
cxf-core-3.1.1.jar
cxf-rt-bindings-soap-3.1.1.jar
cxf-rt-bindings-xml-3.1.1.jar
cxf-rt-databinding-jaxb-3.1.1.jar
cxf-rt-frontend-jaxws-3.1.1.jar
cxf-rt-frontend-simple-3.1.1.jar
cxf-rt-transports-http-3.1.1.jar
cxf-rt-ws-addr-3.1.1.jar
cxf-rt-ws-policy-3.1.1.jar
cxf-rt-wsdl-3.1.1.jar
jaxb-core-2.2.11.jar
jaxb-impl-2.2.11.jar
neethi-3.0.3.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.1.jar
wsdl4j-1.6.3.jar
xml-resolver-1.2.jar
xmlschema-core-2.2.1.jar
2. 创建相关类
2.1 IHelloWorld
接口
@WebService
interface IHelloWorld {String sayHello(@WebParam(name = "username") String username);Map<String, Object> getObj(@WebParam(name = "username")String username, @WebParam(name = "sexy")String sexy);
}
2.2 HelloWorldImpl
实现类
public class HelloWorldImpl implements IHelloWorld { @Override public String sayHello(String username) { return "Hello " + username; } @Overridepublic Map<String, Object> getObj(String username, String sexy) {Map<String, Object> customer = new HashMap<String, Object>();customer.put("NAME", username);customer.put("SEXY", sexy);customer.put("BIRTHDAY", Calendar.getInstance().getTime());return customer;}
}
3. Spring配置
spring-cxf-service.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:http-conf="http://cxf.apache.org/transports/http/configuration"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 http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"><!-- 以下这个文件位于cxf-core-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf.xml" /><!-- 以下这个文件位于cxf-rt-databinding-jaxb-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloService"implementor="com.kq.webservice.impl.HelloWorldImpl"address="/helloService" />
</beans>
4. web.xml
<!-- 载入上面配置的spring-cxf-service.xml文件 -->
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring.xml;<!-- cxf配置,必须在这里;这里涉及到的是SpringMVC中child,parent container的加载问题 -->classpath:config/spring-cxf-service.xml; </param-value>
</context-param><!-- 配置cxf相关的servlet -->
<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>
5. 访问
获取该路径所有的服务列表
// 格式 http://host:port/context_path/services// sample http://localhost:9090/SpringMVCDemo/webservice
读取wsdl文件内容
// 格式 http://host:port/context_path/services/serviceName?wsdl// sample http://localhost:9090/SpringMVCDemo/webservice/helloService?wsdl
6. 测试
6.1 单独的测试项目
- 引入相同的cxf相关依赖.
- 唯一需要注意的是webservice接口名彼此必须一模一样.
6.2 基于SoapUI进行接口测试
- 提取出指定的wsdl文件,按照上面的方式提取出指定的wsdl内容,复制到本地的文件中.
- 使用提取到的wsdl文件创建soapUI工程.
6.3 编写测试类
此种方式比较方便,无须配置比较灵活,大部分情况下都会采用这种方式调用服务程序。
返回简单的string
private static void test_returnSimpleString(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();String response = client.sayHello("LQ");System.out.println("Response:" + response); }
返回复杂对象
private static void test_returnCustomObj(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();Map<String, Object> obj = client.getObj("LQ","1");System.out.println(obj); }
返回JSON
未完成
7. 疑难问题
Exception in thread "main" **org.apache.cxf.common.i18n.UncheckedException**: No operation was found with the name {http://impl.server.test.com/}helloWorld.
- 参考链接 : https://www.cnblogs.com/yshyee/p/3633537.html
解决方法:对服务端的接口实现类中的
@WebService
添加targetNamespace
,其值为接口包名的倒置。例如我的IHelloWorld
接口所在的包为com.test.server
,此时对应的targeNamespace
的值为http://server.test.com/
@WebService(endpointInterface = "com.test.server.IHelloWorld", serviceName="helloWorld", targetNamespace="http://server.test.com/") public class HelloWorldImp implements IHelloWorld {public String helloWorld(String name) {return name+" Hello,World!";}}
8. 参考链接
http://cxf.apache.org/docs/writing-a-service-with-spring.html - 官方文档
http://blog.csdn.net/blueheart20/article/details/42971713
- 有几处问题.基本是因为cxf的版本问题导致的。
- 关于webservice相关的理论可以在此文章中看看。
cxf-spring-pratice-service相关推荐
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service...
起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)--不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...
- Spring Web Service 学习之Hello World篇
http://fuxueliang.iteye.com/blog/175184 Spring Web Service是Spring社区基于Spring提供的一个关注于创建"文档驱动" ...
- CXF 生成Web Service Client(将WSDl 转化成 Java代码)
CXF 是什么 Apache CXF一个开源的Service框架,它实现了JCP与Web Service中一些重要标准. CXF简化了构造,集成,面 向服务架构(SOA)业务组件与技术的灵活复用.在C ...
- Apache CXF实现Web Service(5)—— GZIP使用
Apache CXF实现Web Service(5)-- GZIP使用 参考来源: CXF WebService整合Spring Apache CXF实现Web Service(1)--不借助重量级W ...
- java webservice超时设置_[CXF]Spring下设置CXF的WebService客户端超时时长
评论 # re: [CXF]Spring下设置CXF的WebService客户端超时时长 2010-01-05 21:47 Emily32Av A kind of good information a ...
- java service注入失败,使用spring向service里面注入dao不成功。
使用spring向service里面注入dao不成功.求救啊! 本帖最后由 PaperStar 于 2013-12-26 19:29:20 编辑 页面调用action,action调用service, ...
- JAXWS CXF Spring + MyEclipse + Maven + Tomcat Byron自學視頻02
JAXWS CXF Spring + MyEclipse + Maven + Tomcat Byron自學視頻02 Description: 使用 Apache CXF 建置 WebService 並 ...
- Spring开发Service层
Spring开发Service层 service层的作用: 业务逻辑操作(某些操作依赖于Dao层),将操作后的结果在返回出去给View层根据结果展现视图效果. 在开发完dao层(Spring+MyBa ...
- Web Service——CXF+Spring 整合
结合spring框架来实现CXF发布SOAP协议的服务,步骤基本相同,所不同的是的多了一些配置项,步骤如下 1. 服务端 第一步:创建web项目(引入jar包) 第二步:创建SEI接口 import ...
- Java使用Apache CXF开发Web Service
转自:http://blog.csdn.net/hu_shengyang/article/details/38384597 以前工作中也用CXF,但都是用别人现成搭好的环境,这次自己重头搭建一遍环境. ...
最新文章
- DATETIME类型和BIGINT 类型互相转换
- 限制会话id服务端不共享_会话控制 - able-woman - 博客园
- oracle多次发运,Oracle EBS 关于发运收集整理
- 剑指Spring源码(一)
- Android 开发工具类 36_ getSimSerial
- JAVA 计算地球上任意两点(经纬度)距离
- 3.4 卷积的滑动窗口实现
- 你看,Fastjson 漏洞也太多了吧。。
- [ios] - TommyBros(山寨马里奥) – 开源游戏
- Spring内异常 application exception overridden by commit exception
- 新媒体运营模式的发展历史
- pycharm: Error: Cannot run program……
- LeetCode-自除数
- 将你的笔记本变成无线路由器
- 数据库事务 ACID
- ⑰霍兰德EI*如何选选专业?高考志愿填报选专业
- pymysql——excel数据导入mysql
- java判断string是数字_(转)java判断string变量是否是数字的六种方法小结
- 【资源】可下载三维模型的网站
- linux WordPress安装