天天看点

使用CXF+spring+restful创建一个web的接口项目

此文为http://blog.csdn.net/zxnlmj/article/details/28880303的下文,在其基础上添加restful功能

1、添加restful的所需jar包

本文选择cxf-2.4.2.jar与jsr311-api-1.0.jar

2、开发restful服务

新建RestfulRegeditService.java接口

新建RestfulRegeditServiceImpl.java接口

3、修改spring配置文件applicationContext.xml(粗体部分为需添加的)

<?xml version="1.0"?>

<beans xmlns="http://www.springframework.org/schema/b<span style="background-color: rgb(255, 255, 0);">eans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc</span>e" xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                        http://www.springframework.org/schema/aop

                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

                        http://www.springframework.org/schema/context

                        http://www.springframework.org/schema/context/spring-context-3.0.xsd

                        http://www.springframework.org/schema/tx

                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.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" />

    <!-- webservice配置 ,myeclipse检测到此处有错没影响-->

    <jaxws:endpoint id="regeditService" implementor="zxn.ws.service.RegeditServiceImpl" address="/Regedit" />

<bean id="restfulRegeditService" class="zxn.ws.service.impl.RestfulRegeditServiceImpl" />

<!--restful服务 -->

    <jaxrs:server id="restServiceContainer" address="/regedit">

        <jaxrs:serviceBeans>

            <ref bean="restfulRegeditService" />

        </jaxrs:serviceBeans>

        <jaxrs:extensionMappings>

            <entry key="json" value="application/json" />

            <entry key="xml" value="application/xml" />

        </jaxrs:extensionMappings>

        <jaxrs:languageMappings>

            <entry key="en" value="en-gb" />

        </jaxrs:languageMappings>

    </jaxrs:server></strong>

</beans>

4、部署到tomcat,运行,成功,界面下图:

使用CXF+spring+restful创建一个web的接口项目

5、源代码地址下载:http://download.csdn.net/detail/zxnlmj/7458403

继续阅读