CXF+Spring官方实例学习

系统 1408 0

周末学习了cxf发布webservice的例子,按照官网上的步骤进行配置,错误不断,经过一番折腾,终于成功了。

下面说说我的配置:

1.下载jar:
CXF+Spring官方实例学习

2.web.xml
    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!--	
	spring需要加载的配置文件
-->
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:com/zhengs/spring-cxf.xml
		</param-value>
	</context-param>
	<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>    
        <load-on-startup>1</load-on-startup>    
    </servlet>    
    <servlet-mapping>    
        <servlet-name>CXFServlet</servlet-name>    
        <url-pattern>/service/*</url-pattern>    
    </servlet-mapping>    
</web-app>
  

3.spring-cxf.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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:cxf="http://cxf.apache.org/core"
	xmlns:wsa="http://cxf.apache.org/ws/addressing"
	xsi:schemaLocation="   
	  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.1.xsd
	 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
	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">
	<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" />
	
	 <cxf:bus>
        <cxf:features>
        	<!--日志拦截功能,用于监控soap内容,开发后可以删除 --> 
            <cxf:logging/>
            <wsa:addressing/>
        </cxf:features>
    </cxf:bus>  
	<bean id="hello" class="com.zhengs.HelloWorldImpl" />

	<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" publish="true"/>

</beans>
  


4.webservice接口HelloWorld.java
    package com.zhengs;

import javax.jws.WebService;

@WebService(targetNamespace="zhengs.com")
public interface HelloWorld {
    String sayHi(String text);
}

  

实现:HelloWorldImpl.java
    package com.zhengs;

import javax.jws.WebService;

@WebService(endpointInterface = "com.zhengs.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

	public String sayHi(String text) {
		return "Hello , " + text;
	}

}
  

5加上log4j.properties文件,部署到tomcat6.0下面,在5.0.28下面测试过有异常
6.添加测试类Client.java
    
package com.zhengs.client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhengs.HelloWorld2;


public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext("beans.xml");

        HelloWorld2 client = (HelloWorld2)context.getBean("helloClient");

        String response = client.sayHi("Joe");
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}
  

HelloWorld2.java可以用HelloWorld.java代替
结束。
代码打包,提供下载,lib包自行下载,提供网址:http://jarvana.com/jarvana/

CXF+Spring官方实例学习


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论