springboot RESTful以及參數(shù)注解的使用方式
springboot RESTful及參數(shù)注解使用
RESTful
Spring的復(fù)雜性不是來自于它處理的對象,而是來自于自身,不斷演進(jìn)發(fā)展的Spring會帶來時間維度上復(fù)雜性,比如SpringMVC以前版本的@RequestMapping,到了新版本被下面新注釋替代,相當(dāng)于增加的選項:
@GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping
說明
1、@GetMapping
@RequestMapping(method = RequestMethod.GET)的簡寫
作用:對應(yīng)查詢,表明是一個查詢URL映射
2、@PostMapping
@RequestMapping(method = RequestMethod.POST)的簡寫
作用:對應(yīng)增加,表明是一個增加URL映射
3、@PutMapping
@RequestMapping(method = RequestMethod.PUT)的簡寫
作用:對應(yīng)更新,表明是一個更新URL映射
4、@DeleteMapping
@RequestMapping(method = RequestMethod.DELETE)的簡寫
作用:對應(yīng)刪除,表明是一個刪除URL映射
5、@PatchMapping
Patch方式是對put方式的一種補充;
put方式是可以更新.但是更新的是整體.patch是對局部更新;
參數(shù)注解的使用
@PathVariable @RequestParam @RequestBody @ModelAttribute
說明
1. @PathVariable
獲取路徑參數(shù)。即url/{id}這種形式
@PathVariable綁定URI模板變量值
@PathVariable是用來獲得請求url中的動態(tài)參數(shù)的
@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上。//配置url和方法的一個關(guān)系@RequestMapping(“item/{itemId}”)
2.@RequestParam
獲取查詢參數(shù)。即url?name=這種形式
@RequestParam注解主要有哪些參數(shù):
value
:參數(shù)名字,即入?yún)⒌恼埱髤?shù)名字,如username表示請求的參數(shù)區(qū)中的名字為username的參數(shù)的值將傳入;required
:是否必須,默認(rèn)是true,表示請求中一定要有相應(yīng)的參數(shù),否則將報404錯誤碼;defaultValue
:默認(rèn)值,表示如果請求中沒有同名參數(shù)時的默認(rèn)值,例如:
public List getItemTreeNode(@RequestParam(value=“id”,defaultValue=“0”)long parentId)
3.@RequestBody
@requestBody注解常用來處理content-type不是默認(rèn)的application/x-www-form-urlcoded編碼的內(nèi)容,比如說:application/json或者是application/xml等。一般情況下來說常用其來處理application/json類型。
通過@requestBody可以將請求體中的JSON字符串綁定到相應(yīng)的bean上,當(dāng)然,也可以將其分別綁定到對應(yīng)的字符串上。
4.@ModelAttribute
在使用RESTful風(fēng)格時,使用get請求,又想使用對象接收參數(shù),就可以使用這個注解
不光適用于get請求,同樣也適用于put和delete請求
springboot Restful使用記錄
創(chuàng)建項目
通過spring官網(wǎng)創(chuàng)建項目
- 項目名稱取為studyRest
- 項目依賴WEB
Rest組件使用
使用@RestController標(biāo)記類為提供Restful服務(wù)的Contoller
@GetMapping為資源定位一部分,也就是url,對應(yīng)http://localhost:8080/test
@RestController public class MyRestContoller1 { @GetMapping("/test") public Map<String, String> getData() { Map<String, String> data = new HashMap<String, String>(); data.put("id", "111"); data.put("name", "zhangsan"); return data; } }
測試(這里使用瀏覽器測試,后續(xù)使用Postman工具)
@GetMapping關(guān)鍵字對應(yīng)GET請求,也就是查詢,請求還可以有參數(shù),對應(yīng)@PathVariable與@RequestParam注解
@GetMapping("/test/{id}") public Map<String, String> getData2(@PathVariable String id, @RequestParam(required = false) String name) { Map<String, String> data = new HashMap<String, String>(); data.put("id", id); data.put("name", name); return data; }
測試,返回值為入?yún)魅雲(yún)?shù)
Post類型,新增操作
新增使用@PostMapping描述URL
新增一般都會帶有大量數(shù)據(jù),一般都是使用@RequestBody注解封裝參數(shù)
@PostMapping("/test2/add") public Map<String, String> addData(@RequestBody Map<String, String> data) { return data; }
測試
注意兩點,不正確都會報錯
- 請求類型必須是POST
- Content-type必須要設(shè)置為application/json,因為入?yún)⑿问綖镴SON格式
更新與刪除操作
使用上與Post一致,只是不同類型需要使用對應(yīng)的主機
PUT
:@PutMappingDELETE
:@DeleteMapping
@PutMapping("/test2/update") public Map<String, String> updateData(@RequestBody Map<String, String> data) { return data; } @DeleteMapping("/test2/delete") public Map<String, String> deleteData(@RequestBody Map<String, String> data) { return data; }
RequestMapping使用
RequestMapping是一個通用注解,包含上述所有操作
@RestController @RequestMapping(value = "/parent") public class RequestRestContoller { @RequestMapping(value = "/get", method = RequestMethod.GET) public Map<String, String> get() { Map<String, String> data = new HashMap<String, String>(); data.put("id", "111"); data.put("name", "zhangsan"); return data; } @RequestMapping(value = "/add", method = RequestMethod.POST) public Map<String, String> add() { Map<String, String> data = new HashMap<String, String>(); data.put("id", "111"); data.put("name", "zhangsan"); return data; } @RequestMapping(value = "/update", method = RequestMethod.PUT) public Map<String, String> update() { Map<String, String> data = new HashMap<String, String>(); data.put("id", "111"); data.put("name", "zhangsan"); return data; } @RequestMapping(value = "/delete", method = RequestMethod.DELETE) public Map<String, String> delete() { Map<String, String> data = new HashMap<String, String>(); data.put("id", "111"); data.put("name", "zhangsan"); return data; } }
上述還有貼在class上面的注解:@RequestMapping(value = "/parent"),如果是class上面的注解,那么方法上面的url需要加上class上面的注解
如:http://localhost:8080/parent/get或http://localhost:8080/parent/add
其中可以屬于請求參數(shù)和響應(yīng)數(shù)據(jù)類型
@RequestMapping(value = "/parent", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
其中consumes 約束入?yún)㈩愋?,produces 約束響應(yīng)數(shù)據(jù)類型
測試Content-Type:text/plain報錯,由于設(shè)置了JSON格式
支持哪些格式參考Media定義
org.springframework.http.MediaType
XML格式數(shù)據(jù)支持
這里擴展一下,返回XML格式數(shù)據(jù)
引入XML依賴包
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </dependency>
測試類
@RestController public class DataRestContoller { @RequestMapping(value = "/addJsonResponseXml", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_XML_VALUE) public Map<String, String> add(@RequestBody Map<String, String> data) { return data; } }
測試
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用IDEA創(chuàng)建java項目的步驟詳解(hello word)
這篇文章主要介紹了使用IDEA創(chuàng)建java項目的步驟詳解(hello word),本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12java中javamail發(fā)送帶附件的郵件實現(xiàn)方法
這篇文章主要介紹了java中javamail發(fā)送帶附件的郵件實現(xiàn)方法,較為詳細(xì)的分析了JavaMail發(fā)送郵件的用法,是非常實用的技巧,需要的朋友可以參考下2015-01-01java+vue實現(xiàn)添加單選題、多選題到題庫功能
這篇文章主要為大家詳細(xì)介紹了java+vue實現(xiàn)添加單選題、多選題到題庫功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04java基于雙向環(huán)形鏈表解決丟手帕問題的方法示例
這篇文章主要介紹了java基于雙向環(huán)形鏈表解決丟手帕問題的方法,簡單描述了丟手帕問題,并結(jié)合實例形式給出了Java基于雙向環(huán)形鏈表解決丟手帕問題的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11SpringBoot項目集成Flyway進(jìn)行數(shù)據(jù)庫版本控制的詳細(xì)教程
這篇文章主要介紹了SpringBoot項目集成Flyway進(jìn)行數(shù)據(jù)庫版本控制,本文分步驟通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07