亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringMVC Restful api接口實現(xiàn)的代碼

 更新時間:2017年09月22日 09:05:36   作者:柒小棧主  
本篇文章主要介紹了SpringMVC Restful api接口實現(xiàn)的代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

【前言】

面向資源的 Restful 風格的 api 接口本著簡潔,資源,便于擴展,便于理解等等各項優(yōu)勢,在如今的系統(tǒng)服務中越來越受歡迎。

.net平臺有WebAPi項目是專門用來實現(xiàn)Restful api的,其良好的系統(tǒng)封裝,簡潔優(yōu)雅的代碼實現(xiàn),深受.net平臺開發(fā)人員所青睞,在后臺服務api接口中,已經(jīng)逐步取代了輝煌一時MVC Controller,更準確地說,合適的項目使用更加合適的工具,開發(fā)效率將會更加高效。

python平臺有tornado框架,也是原生支持了Restful api,在使用上有了很大的便利。

Java平臺的SpringMVC主鍵在Web開發(fā)中取代了Struts2而占據(jù)了更加有力的地位,我們今天著重講解如何在Java SpringMVC項目中實現(xiàn)Restful api。

【實現(xiàn)思路】

Restful api的實現(xiàn)脫離不了路由,這里我們的Restful api路由由spring mvc 的 controller來實現(xiàn)。

【開發(fā)及部署環(huán)境】

開發(fā)環(huán)境:Windows 7 ×64 英文版

     Intellij IDEA 2017.2

部署環(huán)境:JDK 1.8.0

     Tomcat 8.5.5

測試環(huán)境:chrome

        fiddler

【實現(xiàn)過程】

1、搭建spring mvc maven項目

這里的搭建步驟不再贅述,如有需要參考:http://chabaoo.cn/article/117670.htm

2、新建控制器 StudentController

為了體現(xiàn)Restful api 我們采用注解,RequestMapping("/api/Student")

具體的代碼如下:

package Controllers;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/Student")
public class StudentController {

 @RequestMapping(method = RequestMethod.GET)
 public String Get() {
  return "{\"id\":\"1\",\"name\":\"1111111111\"}";
 }

 @RequestMapping(method = RequestMethod.POST)
 public String Post() {
  return "{\"id\":\"2\",\"name\":\"2222222222\"}";
 }

 @RequestMapping(method = RequestMethod.PUT)
 public String Put() {
  return "{\"id\":\"3\",\"name\":\"3333333333\"}";
 }

 @RequestMapping(method = RequestMethod.DELETE)
 public String DELETE() {
  return "{\"id\":\"4\",\"name\":\"4444444444\"}";
 }

 @RequestMapping(value = "/{id}",method = RequestMethod.GET)
 public String Get(@PathVariable("id") Integer id) {
  return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}";
 }
}

這里有Get,Post,Put,Delete分別對應 查詢,添加,修改,刪除四種對資源的操作,即通常所說的CRUD。

spring mvc可實現(xiàn)restful的方式有@Controller和@RestController兩種方式,兩種方式的區(qū)別如下:

@Controller的方式實現(xiàn)如果要返回json,xml等文本,需要額外添加@ResponseBody注解,例如: 

   @ResponseBody //用于返回json數(shù)據(jù)或者text格式文本
  @RequestMapping(value = "/TestJson", method = RequestMethod.GET)
  public String TestJson() {
   return "{\"id\":\"1001\",\"name\":\"zhangsan\"}";
  }

@RestController方式不需要寫@ResponseBody,但是不能返回模型綁定數(shù)據(jù)和jsp視圖,只能返回json,xml文本,僅僅是為了更加方便返回json資源而已。

上述的Rest方法中多寫了個Get方法: 

 @RequestMapping(value = "/{id}",method = RequestMethod.GET)
  public String Get(@PathVariable("id") Integer id) {
   return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}";
  }

該方法可以直接在url拼接一個參數(shù),更加方便對資源的定向訪問,例如查一個student list 可以默認空參數(shù),而查詢對應的某一個student詳情信息,可以id=studentId 定向查詢單條,使得我們對資源的訪問更加快捷方便。

【系統(tǒng)測試】

運行系統(tǒng),使用fiddler調(diào)用restful api接口:

1.Get方式

  

2.Post方式

   

3.Put方式

  

4.Delete方式

   

5.Get/id方式

   

至此,可見我們的spring mvc Restful api接口已經(jīng)全部通過測試!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論