简单WEB Service接口实现(CXF与spring整合)
2015-04-16 15:28
579 查看
最近写接口,之后就对接口的简单实现做个小结:
可以在传统的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接口就成功编写和启动成功,就可以编写客户端来访问该接口啦!
可以在传统的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接口就成功编写和启动成功,就可以编写客户端来访问该接口啦!
相关文章推荐
- CXF-10: CXF 与 Spring 的整合 之 使用别人暴露的 Web Service 接口
- CXF-09: CXF 与 Spring 的整合 之 暴露 Web Service 接口
- Spring整合CXF 发布webservice接口服务器(普通及REST)和客户端,WSDL简单解析
- Web Service的CXF实现(Spring整合方式)
- CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
- Spring 3.0.5 + CXF 2.4 + Hibernate 3.6.0 + MySQL 5.5 整合(CXF/Spring/Hibernate)实现简单的WSS4J认证 + 测试
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- struts-2.3.14.3 + hibernate-4.2.2.Final + spring-3.2.4 整合实现简单注册功能始末
- 使用cxf发布rest服务接口,和spring的整合
- [增删改查] SpringBoot 整合 Solr 之 SolrClient 实现 CRUD、分页接口、高亮显示
- Spring整合ActiveMQ实现简单的消息队列
- WebService-CXF和spring整合实现webservice案例(Spring配置注解环境)
- SpringBoot+Maven项目实战(6):整合Log4j和Aop,实现简单的日志记录
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成(实战)
- Android开发中调用Spring CXF整合发布的WebService接口为什么抛出异常错误?
- CXF实现和Spring整合发布WebService
- java cxf实现webservice接口方式之不依赖spring
- SpringMVC+Mybatis整合实现简单权限控制系统代码
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建