简单WEB Service接口实现(CXF与spring整合)
来源:互联网 发布:python 下划线转驼峰 编辑:程序博客网 时间:2024/05/20 05:56
最近写接口,之后就对接口的简单实现做个小结:
可以在传统的JAVA EE的基础上增加一层Web Service层,这样我们的JAVA EE就可以对外暴露Web Service,这样任何平台、任何语言就可以通过程序来调用此接口了!
具体实现步骤:
1、添加CXF的相关jar包和spring的相关jar包(具体jar包根据自己的实际需求情况而定);
commons-logging.jar
cxf.jar
neethi.jar
wsdl4j.jar
xmlschema-core.jar
spring.jar
spring-asm.jar
spring-expression.jar
2、在WEB.xml中配置CXF的核心控制器,如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
</web-app>
3、在spring的配置文件中导入CXF提供的Schema、XML配置文件;在spring的配置文件中使用axws:endpoint元素来暴露WEB Service。具体如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.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"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<jaxws:endpoint id="cxfSpringService" address="/cxfSpringService" implementorClass="com.cxf.spring.service.CXFSpringService">
<jaxws:implementor>
<bean id="cXFSpringServiceImpl" class="com.cxf.spring.service.impl.CXFSpringServiceImpl"/>
</jaxws:implementor>
</jaxws:endpoint>
</beans>
4、书写简单的WEB Service层:
接口:CXFSpringService
package com.cxf.spring.service;
import javax.jws.WebService;
@WebService
public interface CXFSpringService {
public String cxfTest1();
public void cxfTest2();
}
接口实现:CXFSpringServiceImpl
package com.cxf.spring.service.impl;
import javax.jws.WebService;
import com.cxf.spring.service.CXFSpringService;
//endpointInterface定义实现的接口,serviceName定义发布的服务名
@WebService(endpointInterface="com.cxf.spring.service.CXFSpringService",serviceName="CXFSpring")
public class CXFSpringServiceImpl implements CXFSpringService {
public String cxfTest1() {
return "CXFSpringServiceImpl";
}
public void cxfTest2() {
System.out.println("CXFSpringServiceImpl");
}
}
5、部署该WEB应用,启动tomcat,在浏览器中访问地址:http://localhost:8080/WS_cxf_Spring/cxf/cxfSpringService?wsdl。如正常出现相关的xml文件,就证明该接口正常启动:
<?xml version="1.0"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.spring.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" targetNamespace="http://service.spring.cxf.com/" name="CXFSpringServiceService">
到此,一个简单的WEB接口就成功编写和启动成功,就可以编写客户端来访问该接口啦!
- 简单WEB Service接口实现(CXF与spring整合)
- CXF-09: CXF 与 Spring 的整合 之 暴露 Web Service 接口
- CXF-10: CXF 与 Spring 的整合 之 使用别人暴露的 Web Service 接口
- CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
- spring整合web service - 基于apache CXF实现
- CXF [web service]整合spring 总结
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成(实战)
- CXF简单实现web service服务器端
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- 9.Web Service Spring整合CXF之发布服务
- 基于 Spring 整合 CXF 的 Web Service 超时时间配置
- windows C++读取plist文件方法
- 环形数组的最大连续和
- jstl,if...else...
- 负载均衡--大型在线系统实现的关键(下篇)(服务器集群架构的设计与选择)
- System.Web.Caching.Cache
- 简单WEB Service接口实现(CXF与spring整合)
- python heapq
- 工作日志4-16
- 小米不花一分钱把用户做到10000万的秘密
- VMware10中的CentOS6.5命令行安装VMware tools工具启用windows与虚拟机中Linux系统的共享目录
- 2015-第6周项目6-复制模板类(3)
- CombineTextInputFormat用法
- python中字符串连接的三种方式
- 位图文件存储空间的计算