zoukankan      html  css  js  c++  java
  • Jersey RESTful Web服务

    Jersey是一个RESTFUL请求服务JAVA框架,与常规的JAVA编程使用的struts框架类似,它主要用于处理业务逻辑层。与Struts类似,它同样可以和hibernate,spring框架整合。
    由于Struts2+hibernate+spring整合在市场的占有率太高,所以很少一部分人去关注Jersey。所以网上有关于Jersey的介绍很少。但是它确实是一个非常不错的框架。对于请求式服务,对于GET,DELETE请求,你甚至只需要给出一个URI即可完成操作。
    举个简单的例子:如果你想获得服务器数据库中的所有数据;那么你可以在浏览器或者利用Ajax的GET方法,将路径设置好;例如:localhost:8080/Student(项目名称)/studentinfo(项目服务总体前缀)/student(处理student对象的签注)/getStudentInfo(最后前缀)。这样就可以获取所有学生信息。你可以选择GET获取的数据的返回类型:JSON,XML,TEXT_HTML(String)..获取之后,你可以通过JS将这些数据塞到html或者jsp页面上。
    下面是详解:
    web.xml的设置:
    <!--定义Jersey的拦截器 -->
      <servlet>
      <servlet-name>JerseyServlet</servlet-name>
      <servlet-class>
      com.sun.jersey.spi.spring.container.servlet.SpringServlet
      </servlet-class>
      <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <!--服务类所在的文件夹 -->
      <param-value>com.mirrors.action</param-value><!-- 之所以我定义为com.mirrors.action就是说明此包中类的作用类似于struts中action层类的作用--!>
      </init-param>
      </servlet>
      <servlet-mapping>
      <servlet-name>JerseyServlet</servlet-name>
      <url-pattern>/new/*</url-pattern><!--项目服务总体前缀 -->
      </servlet-mapping>
      <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    StudentAction.java一些代码:
    @Component
      @Path("/student")//处理student对象的签注
      public class StudentAction
      {
      private StudentDao studentdao;
      public void setStudentdaoStudentDao studentdao)
      {
      this.studentdao =studentdao;
      }
      @GET//获取方式
      @Path("getStudentInfo")//最后前缀
    @Produces({ MediaType.APPLICATION_JSON })//返回类型为一个Student对象的JSON数组
      public List<Student> getTrade()
    {
      return studentdao.getStudent();
      }
    }
    这样一个GET方式的处理就结束了,接下来就是前台提取方式,你可以通过JS控制JSON数组在页面的呈现方式。
    Jersey共计有4中处理方式,即:@GET,@POST,@DELETE,@PUT。
  • 相关阅读:
    Apache Ant 1.9.1 版发布
    Apache Subversion 1.8.0rc2 发布
    GNU Gatekeeper 3.3 发布,网关守护管理
    Jekyll 1.0 发布,Ruby 的静态网站生成器
    R语言 3.0.1 源码已经提交到 Github
    SymmetricDS 3.4.0 发布,数据同步和复制
    beego 0.6.0 版本发布,Go 应用框架
    Doxygen 1.8.4 发布,文档生成工具
    SunshineCRM 20130518发布,附带更新说明
    Semplice Linux 4 发布,轻量级发行版
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/3801612.html
Copyright © 2011-2022 走看看