spring整合web service - 基于apache CXF实现

一. 概述

可以在传统的Java EE应用的基础上添加一层Web Service层, 我们的Java EE应用就可以对外暴漏Web Service, 这样就允许任何平台、任何语言编写的程序来调用这个Java EE应用


二. 步骤




1. 新建web工程springCXF, 并复制需要的Jar包:见上图

2. 在web.xml中配置CXF的核心控制器: CXFServlet

<?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">
	
    <!-- Spring核心配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/applicationContext.xml</param-value>
	</context-param>
	
	<!-- 加载Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 配置CXF的核心Servlet -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	
	<!-- 为CXF的核心Servlet配置URL -->
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webService/*</url-pattern>
	</servlet-mapping>
</web-app>

3. 在Spring配置文件applicationContext.xml中导入CXF提供的Schema, xml配置文件, 并使用jaxws:endpoint元素来暴露web service

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- 导入CXF为扩展Spring提供的几个XML配置文件 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- 
		id:指定所定义的Bean在Spring容器中的唯一标识。 
		implementor:指定Web Services的实现类,或者引用容器中另一个已有的Bean实例
		address:指定所创建的Web Service的地址,因为CXF不知道该Web应用对应的URL,
				  也不知道Web服务器的监听端口。因此address属性指定的只是一个相对地址,
				 CXF将会在运行时动态确定该Web Services的服务地址。	
	-->
	<jaxws:endpoint id="sayHello" implementor="com.zdp.service.impl.HelloServiceImpl" address="/sayHello"></jaxws:endpoint>
</beans>
4. 接口及实现类:

HelloService

//以@WebService Annotation标注,表明该接口将对应一个Web Services
@WebService
public interface HelloService {
	//定义一个方法,该方法将被暴露成一个Web Services操作
	public void sayHello(String name); 
}
HelloServiceImpl

@WebService(endpointInterface = "com.zdp.service.HelloService")
public class HelloServiceImpl implements HelloService{ 
	public void sayHello(String name) {
		System.out.println("hello " + name + ", current time is " + new Date());
	}
}
5. 启动tomcat服务器

6. 新建一个Java工程cxf_Client, 在命令行进入该工程src目录

输入一下命令: wsdl2java http://localhost:9999/springCXF/webService/sayHello?wsdl

7. 写一个测试类:

public class MyClient {
	public static void main(String[] args) {
		HelloServiceImplService factory = new HelloServiceImplService();
		HelloService helloService = factory.getHelloServiceImplPort(); // 返回一个代理
		helloService.sayHello("zhangsan");
	}
}


三. 配置拦截器

其实配置拦截器就是在applicationContext.xml中增加一个配置, 具体的拦截器类请见上一篇博文: http://blog.csdn.net/zdp072/article/details/29245575

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- 导入CXF为扩展Spring提供的几个XML配置文件 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- 拦截器 -->  
    <bean id="authInterceptor" class="com.zdp.interceptor.AuthInterceptor"/> 
	
	<jaxws:endpoint id="sayHello" implementor="com.zdp.service.impl.HelloServiceImpl" address="/sayHello">
		<jaxws:inInterceptors>  
            <ref bean="authInterceptor"/>  
        </jaxws:inInterceptors> 
	</jaxws:endpoint>
</beans>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建基于Apache CXFweb service,需要按照以下步骤进行: 1. 安装Apache CXF 首先,需要下载并安装Apache CXF。可以从官方网站下载最新版本的CXF,并按照官方文档进行安装。 2. 创建Java类 创建一个包含web service方法的Java类。这个类应该用@WebService注解进行注释。 例如: ``` package com.example; import javax.jws.WebService; @WebService public class HelloWorld { public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 3. 创建CXF配置文件 在项目的resources目录下创建一个名为cxf.xml的文件,用于配置CXF。 例如: ``` <?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" 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"> <jaxws:endpoint id="helloWorld" implementor="com.example.HelloWorld" address="/HelloWorld" /> </beans> ``` 这个文件指定了一个名为helloWorld的web service,它的实现类是com.example.HelloWorld,并且它的地址是/HelloWorld。 4. 配置web.xml 在项目的web.xml文件中添加CXFServlet,并将它映射到/*。 例如: ``` <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>/*</url-pattern> </servlet-mapping> ``` 5. 运行web service 现在可以启动web service并测试它了。可以使用任何支持SOAP协议的客户端(如SoapUI)来测试web service。 启动web service后,在浏览器中输入http://localhost:8080/YourAppName/HelloWorld?wsdl,应该能够看到web service的WSDL文档。 以上就是创建基于Apache CXFweb service的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值