cxf-spring-pratice-service
来源:互联网 发布:sql 自增id 重置 编辑:程序博客网 时间:2024/06/07 02:45
其实吧,这篇博客基本上就是照搬了官方的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.jarcxf-core-3.1.1.jarcxf-rt-bindings-soap-3.1.1.jarcxf-rt-bindings-xml-3.1.1.jarcxf-rt-databinding-jaxb-3.1.1.jarcxf-rt-frontend-jaxws-3.1.1.jarcxf-rt-frontend-simple-3.1.1.jarcxf-rt-transports-http-3.1.1.jarcxf-rt-ws-addr-3.1.1.jarcxf-rt-ws-policy-3.1.1.jarcxf-rt-wsdl-3.1.1.jarjaxb-core-2.2.11.jarjaxb-impl-2.2.11.jarneethi-3.0.3.jarstax2-api-3.1.4.jarwoodstox-core-asl-4.4.1.jarwsdl4j-1.6.3.jarxml-resolver-1.2.jarxmlschema-core-2.2.1.jar
2. 创建相关类
2.1 IHelloWorld
接口
@WebServiceinterface 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; } @Override public 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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <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// samplehttp://localhost:9090/SpringMVCDemo/webservice
读取wsdl文件内容
// 格式http://host:port/context_path/services/serviceName?wsdl// samplehttp://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. 参考链接
http://cxf.apache.org/docs/writing-a-service-with-spring.html - 官方文档
http://blog.csdn.net/blueheart20/article/details/42971713
- 有几处问题.基本是因为cxf的版本问题导致的。
- 关于webservice相关的理论可以在此文章中看看。
0 0
- cxf-spring-pratice-service
- cxf-spring-pratice-client
- cxf+web service(四)cxf+spring集成
- CXF [web service]整合spring 总结
- cxf-Writing a service with Spring
- maven+spring+cxf编写web service
- maven+spring+cxf编写web service (2)
- 使用CXF和Spring搭建Web Service
- Spring CXF Mybatis 发布 Web Service 实例
- 用cxf+spring开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Web Service ---- CXF + SPRING 开发 SOAP服务
- cxf和spring开发web service--服务器端
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Eclipse安装CXF插件开发java web service 集成Spring
- 使用Apache CXF和Spring集成创建Web Service
- 【NGUI】各个组件的应用与数值获取
- post和get请求
- 莫名原因的报错
- sdut 2168 Mathmen 贪心
- java-枚举
- cxf-spring-pratice-service
- 一次SSIS Package的调试经历
- centos 安装node环境
- 关于选择参加人员的返回代码以及工作汇报的sql
- ubuntu如何安装GTK+?
- jquery点击图片放大与缩小
- bootstrap 图片设计,折叠菜单,选项卡
- 稀疏编码最优化解法
- Training Region-based Object Detectors with Online Hard Example Mining