個人博客:蘭陵笑笑生,歡迎瀏覽博客!html
上一章 SpringBoot入門實踐(三)-工程結構與經常使用註解當中,咱們介紹了SpringBoot的項目的工程結構和一些經常使用的註解。本章簡單的講一講API的設計。html5
網上有很是多的關於API設計的介紹,本章內容從本身的實際經驗簡單介紹如何更好的設計API。java
1 任何客戶端都可以調用,須要一個標準,使得客戶端和服務端達成一致,必須簡單spring
2 api獨立於客戶端,api的發展,現有的客戶端能夠繼續運行,不須要修改。json
REST API 是圍繞這資源設計的,資源有一個惟一的標識符,而且使用JSONapi
使用HTTP 定義資源的操做app
在springBoot當中提供了不少的註解,使用這些註解就很容易的實現RESTful的apidom
該註解有6個參數:this
例如請求 表示一個GET請求:url
@RequestMapping(path = "/list",method = RequestMethod.GET, consumes ="application/json") public HttpResponse list() { List<User> user = new ArrayList<>(userMap.values()); return HttpResponse.ok().setData(user); }
等價於@RequestMapping(path = "/**",method = RequestMethod.POST)
等價於@RequestMapping(path = "/**",method = RequestMethod.GET)
等價於@RequestMapping(path = "/**",method = RequestMethod.DELETE)
等價於@RequestMapping(path = "/**",method = RequestMethod.PUT)
URL的定義在每一個公司中都不同,這裏簡單給出一些我本身的實際經驗,url路徑定義:
1 url理解簡單
2 api有版本控制, 版本控制能夠快速迭代並避免無效的請求訪問已更新的接入點
http://www.example.com/api/v{version}/模塊名稱/操做名稱
GET /api/v1/user/get/1
或者
GET /api/v1/user/1
POST /api/v1/user/list
POST /api/v1/user/save
DELETE /api/v1/user/delete/12
或者
DELETE /api/v1/user/12
public class User { private Long id; private String userName; private Long age; ... }
自定義統一返回結果
package com.miroservice.chapter2.common; import com.fasterxml.jackson.annotation.JsonInclude; import java.io.Serializable; import java.util.*; @JsonInclude(JsonInclude.Include.NON_NULL) public class HttpResponse implements Serializable { private long code; private String message; private Long total; private Object data; private List<Object> table; private String requestid; public static final long CODE_SUCCESS = 200; public static final long CODE_ERROR = 500; public static final long CODE_VALIDATE_FAILED = 404; public static final long CODE_UNAUTHORIZED = 401; public static final long CODE_FORBIDDEN = 403; public HttpResponse() { this(200, (String) null); } public HttpResponse(long code, String message) { this.code = CODE_SUCCESS; this.code = code; this.message = message; this.requestid = UUID.randomUUID().toString().replaceAll("-", ""); } public static HttpResponse error(String message) { return new HttpResponse(500, message); } public HttpResponse setData(Object data) { this.data = data; return this; } public HttpResponse data(Object data) { return this.setData(data); } public HttpResponse addListItem(Object item) { if (this.table == null) { this.table = new ArrayList(); } this.table.add(item); return this; } public HttpResponse setTotal(Long total) { this.total = total; return this; } public HttpResponse setTotal(Integer total) { this.total = (long) total; return this; } public static HttpResponse ok() { return new HttpResponse(); } public HttpResponse set(String field, String value) { if (this.data == null || !(this.data instanceof Map)) { this.data = new HashMap(); } ((Map) this.data).put(field, value); return this; } public long getCode() { return this.code; } public HttpResponse setCode(long code) { this.code = code; return this; } public String getMessage() { return this.message; } public HttpResponse setMessage(String message) { this.message = message; return this; } public Long getTotal() { return this.total == null && this.table != null ? Long.valueOf(String.valueOf(this.table.size())) : this.total; } public Object getData() { return this.data; } public List<Object> getTable() { return this.table; } public HttpResponse setTable(List table) { this.table = table; return this; } public HttpResponse table(List table) { return this.setTable(table); } public String getRequestid() { return this.requestid; } public HttpResponse setRequestid(String requestid) { this.requestid = requestid; return this; } }
Controller層
/** * 1 api 的設計 一眼明瞭 * 2 有版本 * 3 控制層統一返回自定義的結果 */ @RestController @RequestMapping("/api/v1/user") public class UserController { static Map<Long, User> userMap = new HashMap<Long, User>(); /** * 列表請求 * @return */ @RequestMapping(method = RequestMethod.GET, consumes = "application/json") @GetMapping(consumes = "application/json") public HttpResponse list() { List<User> user = new ArrayList<>(userMap.values()); return HttpResponse.ok().setData(user); } /** * 保存 * @param user * @return */ @PostMapping("/save") public HttpResponse save(@RequestBody User user) { userMap.put(user.getId(), user); return HttpResponse.ok(); } /** * 獲取單個用戶 * @param id * @return */ @GetMapping("/get/{id}") public HttpResponse get(@PathVariable Long id) { final User user = userMap.get(id); return HttpResponse.ok().setData(user); } /** * 刪除 * @param id * @return */ @DeleteMapping("/delete/{id}") public HttpResponse delete(@PathVariable Long id) { userMap.remove(id); return HttpResponse.ok(); } }
以上就是本期的分享,你還能夠關注本博客的#Spring Boot入門實踐系列!#
參考: #微軟的API實踐#
     #[Spring REST DOC](https://docs.spring.io/spring-restdocs/docs/2.0.3.RELEASE/reference/html5/)#
本文由博客一文多發平臺 OpenWrite 發佈!
個人博客[蘭陵笑笑生] ( http://www.hao127.com.cn/),歡...!