Restful是一種軟體架構風格、設計風格,而不是标準,隻是提供了一組設計原則和限制條件。主要用于用戶端和服務 器互動類的軟體,基于這個風格設計的軟體可以更簡潔,更有層次,更易于實作緩存機制等。
例如:
Restful風格的請求是使用“url+請求方式”表示一次請求目的的,HTTP 協定裡面四個表示操作方式的動詞如下:
GET:用于擷取資源
POST:用于建立資源
PUT:用于更新資源
DELETE:用于删除資源
上述url位址/user/1中的1就是要獲得的請求參數,在SpringMVC中可以使用占位符進行參數綁定。
位址/user/1可以寫成 /user/{id},占位符{id}對應的就是1的值。在業務方法中我們可以使用@PathVariable注解進行占位符的比對擷取工作。
例如:
/user/1 GET : 得到 id = 1 的 user
/user/1 DELETE: 删除 id = 1 的 user
/user/1 PUT: 更新 id = 1 的 user
/user POST: 新增 user
下面針對 GET 來一次測試:
package com.bihu.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
//通路http://localhost:8080/ok/張三 那麼 其中張三就會注入到 占位符 name 裡面 然後下面的形參name就會張三了
//下面的name是占位符
@RequestMapping("/ok/{name}")
@ResponseBody//直接響應 不進行跳轉
//下面value代表占位符的值注入到此,required就是參數允許為空
public void post(@PathVariable(value = "name",required = false) String name){
System.out.println(name);
}
}
這個是風格而已 ;了解即可,後面待補充 其他方式我也是不會的 因為沒怎麼用 幾乎不用