WebService学习笔记(六)Spring与CXF整合服务端

1.新建Web工程,导入spring-3.2.8包与cxf2.7包,cxf2.x与cxf3.x差别不小,就像spring-2.x与spring3.x一样,cxf3.x把原cxf.jar分离出了N个jar包

cxf依赖包:

asm-3.3.1.jar

wsdl4j-1.6.3.jar

commons-logging-1.2.jar

cxf-2.7.17.jar

neethi-3.0.3.jar

xmlschema-core-2.1.0.jar

stax2-api-3.1.4.jar

woodstox-core-asl-4.4.1.jar

2.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- spring配置 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:config/spring-config.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- cxf配置,添加CXFServlet -->
<servlet>
	<servlet-name>CXF</servlet-name>
	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	<load-on-startup>1</load-on-startup>  
</servlet>
<!-- 捕获/doWS下的地址,wsdl地址要加上这个,开始没有这样做,浪费了一天的时间  -->
<servlet-mapping>
	<servlet-name>CXF</servlet-name>
	<url-pattern>/doWS/*</url-pattern>
</servlet-mapping>

</web-app>

注意:CXFServlet配置了url-pattern后,需要在wsdl地址中要加上这个配置:如要捕获的url的是/doWS/*,原wsdl地址是http://192.168.2.102:8080/spring_cxf/HelloWorld?wsdl,新的wsdl地址应该是http://192.168.2.102:8080/spring_cxf/doWS/HelloWorld?wsdl


3.spring容器:spring-config.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:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
	http://cxf.apache.org/jaxws   
	http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 导入cxf 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="hello" class="com.skymr.hello.ws.impl.HelloWorldBean"/>  
    <!-- 先导入命名空间和配置规范
	http://cxf.apache.org/jaxws   
	http://cxf.apache.org/schemas/jaxws.xsd
     -->
    <!-- implementor
    	可以是类全名,这样的话它的实例是new的,不受spring容器管理,不好
    	可以引用spring容器里的bean
    	address
    	wsdl的相对路径,不能是绝对路径,绝对路径是随服务器地址变动的。
     -->
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />  
</beans>

命名空间在cxf-2.x.jar包中的schema/jaxws.xsd找到;

规范地址在cxf-2.x.jar包中的spring.schemas文件中找到。

3.要暴露的接口

package com.skymr.hello.ws;

import javax.jws.WebService;


@WebService
public interface HelloWorld {
	
	public String sayHello(String name);
	
}



4.接口实现类

package com.skymr.hello.ws.impl;

import javax.jws.WebService;

import com.skymr.hello.ws.HelloWorld;
@WebService(endpointInterface="com.skymr.hello.ws.HelloWorld",serviceName="helloWorldws")
public class HelloWorldBean implements HelloWorld{

	public String sayHello(String name) {
		return "你好"+name+",欢迎进入Web Service学习课程";
	}


}



5.测试

启动服务器,测试wsdl地址:http://localhost:8080/spring_cxf/doWS/HelloWorld?wsdl,使用wsdl2java工具生成客户端代码测试成功。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值