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

此文为使用CXF+spring创建一个web的接口项目的下文,在其基础上添加restful功能

1、添加restful的所需jar包

jsr311-api-1.0.jar

CXF与JAX-RS版本对应问题,参考自:http://bioubiou.iteye.com/blog/1866871
CXF支持REST风格的Web服务:JAX-RS2.0(JSR-339)和JAX-RS1.1(JSR-311)的Java API。
CXF2.7.0支持JAX-RS2.0(不包括客户端API现在 - 引入的新功能,但注意CXF客户端API已经更新,支持新的过滤器,拦截器,异常类和响应API,再加上客户端的异步调用API)。
CXF2.6.x版本,在2.5.x,2.4.x和2.3.x的支持JSR-311 API1.1和JAX-RS1.1 TCK符合。
CXF2.2.x的支持JSR-311 API1.0和JAX-RS1.0 TCK标准。
CXF的2.1.x支持JSR-311 API0.8。

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

2、开发restful服务

新建RestfulRegeditService.java接口

package zxn.ws.service;  

import java.io.IOException;  

import javax.ws.rs.Consumes;  
import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.core.MediaType;  

@Path(value = "/")  
public interface RestfulRegeditService {  

    @POST  
    @Path("/regedit")  
    @Consumes(MediaType.APPLICATION_JSON)  
    public String regedit(String username, String password) throws IOException;  

}  

新建RestfulRegeditServiceImpl.java接口

package zxn.ws.service.impl;  

import java.io.IOException;  

import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.Context;  
import javax.ws.rs.core.MediaType;  
import javax.ws.rs.core.Request;  
import javax.ws.rs.core.UriInfo;  

import zxn.ws.service.RestfulRegeditService;  


@Path(value = "/")  
public class RestfulRegeditServiceImpl implements RestfulRegeditService {  

    @Context  
    private UriInfo uriInfo;  

    @Context  
    private Request request;  

    @POST  
    @Path("/regedit")  
    @Produces(MediaType.APPLICATION_JSON)  
    public String regedit(String username, String password) throws IOException {  
        return "";  
    }   
}  

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,运行,成功,界面下图:

这里写图片描述

5、源代码地址下载:

CXF+spring+restful创建一个web的接口项目源代码

6、其他参考资料

Spring整合CXF,发布RSETful 风格WebService

这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的。关于发布CXF WebServer和Spring整合CXF这里就不再多加赘述了。如果你对Spring整合CXF WebService不了解,具体你可以参看这两篇文章:

http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html

http://www.cnblogs.com/hoojo/archive/2012/07/13/2590593.html

如果你不了解restful风格的WebService,你可以参考:

http://www.oracle.com/technetwork/articles/javase/index-137171.html

SpringMVC对RESTful的支持:

http://www.cnblogs.com/hoojo/archive/2011/06/10/2077422.html

使用 Jersey框架,搭建RESTful WebService(这个也比较简单)

http://www.ibm.com/developerworks/cn/web/wa-aj-tomcat/

官方文档:http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e8

其中,比较常用的RESTful框架就有Jersey、Spring REST、CXF RESTful,这些都可以很好的整合Spring框架,发布也相当的简单。且简单、易用、易上手,文档也比较丰富。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值