Jersey RESTful Web服務

來源:互聯網
上載者:User

標籤:class   java   ext   com   get   使用   

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。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.