qq_28462581 2015-06-03 07:11 采纳率: 100%
浏览 1896
已采纳

求一套简单的cxf+spring restful的源码

能够通过简单的html get方式访问并传值就可以了

接口用@path定义,一经采纳立马给悬赏,可追加!急,在线等,谢谢了

  • 写回答

1条回答

  • WorldMobile 2015-06-03 07:42
    关注

    试试以下例子

    cxf、spring集成webservice 配置restful方式
    刚刚配置完spring集成cxf使用restful方式部署webservice,整个过程感觉就是爽和简单,欢迎抛砖引玉
    第一步:当然是下载jar包了
    使用到的jar有以下:
    1、spring jar包我就不说了,地球人都知道
    2、cxf-2.6.9.jar 最新版是2.7X版本,我开始下的2.7版本,发现支持的JSR是2.0,起码需要JSR-339.api.jar,但是我下的是jsr311-api-1.1.1.jar,只能支持2.7以下版本,后来又重新下了cxf2.6.9在这里记录下避免忘记了。
    3、jsr311-api-1.1.1.jar
    4、geronimo-servlet_3.0_spec-1.0.jar
    5、neethi-3.0.2.jar
    第二步: 配置项目中web.xml

    CXFService
    org.apache.cxf.transport.servlet.CXFServlet


    CXFService
    /*

    第三步:
    新建appalicationcontext_cxf.xml
    内容如下
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    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
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd">



    jaxrs:serviceBeans

    /jaxrs:serviceBeans
    jaxrs:extensionMappings


    /jaxrs:extensionMappings
    jaxrs:languageMappings


    /jaxrs:languageMappings
    /jaxrs:server

    下面就是例子
    新建Sample接口类
    @Path(value = "/sample")
    public interface Sample {

    @GET
    @Path("/bean/{id}")
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public String getBean(@PathParam("id") int id);

    @GET
    @Path("/list/{id}")
    @Produces({ "application/xml","application/json" })
    public String getBean2(@PathParam("id") int id);
    }

    新建Sample接口的实现类
    @Path(value = "/sample")
    public class RESTSampleSource implements Sample {

    @Override
    @GET
    @Path("/bean/{id}")
    @Produces({ "application/xml", "application/json" })
    public String getBean(@PathParam("id") int id) {
    return "1";
    }

    @Override
    @GET
    @Path("/list")
    @Produces({ "application/xml","application/json" })
    public String getBean2(@PathParam("id") int id) {
    return "{id:'123'}";
    }

    }

    好了,启动发布
    使用
    http://localhost:8080/cxfWebservice/rest/sample/bean/123 默认返回xml
    http://localhost:8080/cxfWebservice/rest/sample/list?id=1&_type=json 加入_type=json则可以返回json数据

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?