码迷,mamicode.com
首页 > 编程语言 > 详细

CXF3.0.4与spring整合开发Webservice功能的web项目

时间:2015-03-31 18:05:58      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:cxf3-0-4   spring   spring与cxf   

项目非常简单,项目结构如下:
技术分享
就一个HelloWorldDao接口,一个实现类HelloWorldImpl,一个spring的配置文件applicationContext-server.xml,一个web项目的配置文件web.xml。(当然需要导入CXF3.0.4的包,以及spring的相关包,还有一些像common-logging,aopalliance这样的web项目必备的包,顺便提一下,我的开发环境是jdk1.8.0_40,tomcat7.0.59,我一般喜欢用最新版的!)具体包的截图如下:()
技术分享
接下来就是spring的包:(其实spring的包也用不完,如和aop相关的)
技术分享
接下来看代码和配置文件部分:
1: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"
    version="2.5">

    <!-- 设置Spring容器加载配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-server.xml</param-value>
    </context-param>

    <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>
</web-app>  

2:applicationContext-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
        http://www.springframework.org/schema/context    
        http://www.springframework.org/schema/context/spring-context-3.0.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" /> -->

    <bean id="helloWorldImpl" class="com.java.service.impl.HelloWorldImpl" />

    <!-- 2:通过jaxws:server方式来配置webservice -->
    <jaxws:server serviceClass="com.java.service.HelloWorldDao"
        address="/helloworld">
        <jaxws:serviceBean>
            <ref bean="helloWorldImpl" />
        </jaxws:serviceBean>
    </jaxws:server>
</beans>    

3:HelloWorldDao

package com.java.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorldDao {
    public void sayHello(@WebParam(name = "name") String name);

}

4:HelloWorldImpl

package com.java.service.impl;

import javax.jws.WebService;
import com.java.service.HelloWorldDao;

@WebService
public class HelloWorldImpl implements HelloWorldDao {

    @Override
    public void sayHello(String name) {
        System.out.println("hello," + name);
    }
}

在网上查了很多资料,CXF以前的版本需要引入3个xml文件,但是通过测试,只需要引入

<import resource="classpath:META-INF/cxf/cxf.xml" />

即可!

最后进行测试:

注意,我们在web.xml中配置了webservice的拦截器:

    <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>

即凡是与/webservice开头的url都可以交给CXFservice处理。于是在tomcat下面启动项目后,打开浏览器,输入:
http://localhost:8080/CXF3.0.4_Webservice/webservice
结果如下:
技术分享
点击wsdl后面的超链接,结果如下:
技术分享
测试成功。

CXF3.0.4与spring整合开发Webservice功能的web项目

标签:cxf3-0-4   spring   spring与cxf   

原文地址:http://blog.csdn.net/u010523770/article/details/44781931

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!