SpringBoot三部曲之Controller統一返回ResponseData
使用SpringBoot構建WEB工程,由於其三大特性,使我們把注意力更多的放在核心功能呢的編寫上,這也要求我們寫出更具規範的程式碼。
我會從三個方面去加強我的程式碼規範
Controller統一返回
Controller請求AOP切面日誌處理(引數校驗,rsa校驗)
Controller統一異常處理
這篇我們先說Controller統一返回ResponesData<T>
如果你和你的後端同學配合寫過介面,可能會面裡下面的尷尬情況:
@RestController
@RequestMapping("example")
public class ExampleController {
@GetMapping("str")
public String str() {
return "Result Type is String";
}
@GetMapping("map")
@ResponseBody
public Map<String, Object> map() {
Map<String, Object> hashMap = new HashMap<>(1);
hashMap.put("Result", "Map");
return hashMap;
}
@PostMapping("none")
public void none() {
System.out.println("none");
}
}
每一種返回的結果都不同。?????
我的請求要怎麼寫。。。
如果使用了統一返回,結果會變得簡單,處理起來十分輕鬆,後端程式碼也變得及其簡潔。
@RestController
@RequestMapping("gua")
public class GuaController {
@GetMapping("str")
public ResponseData str() {
return ResponseDataUtil.buildSuccess("Result String");
}
@GetMapping("data")
public ResponseData data() {
return ResponseDataUtil.buildSuccess(new User());
}
@GetMapping("map")
public ResponseData map() {
HashMap<String, Object> map = new HashMap<>(1);
map.put("Result", "Map");
return ResponseDataUtil.buildSuccess(map);
}
}
其返回結果:
{"code":"0000","msg":"Result String","data":null}
{"code":"0000","msg":"請求成功","data":{"name":"DongGua","age":18}}
{"code":"0000","msg":"請求成功","data":{"Result":"Map"}}
需要返回得資料全部放到data,讓一切變得規範和簡潔。
實現統一返回非常簡單,只需要三個功能
ResponseData<T>
ResponseDataUtils
ResultEnum
三者得結構得非常簡單:ResponseData<T>
public class ResponseData<T> implements Serializable {
private String code;
private String msg;
private T data;
public ResponseData(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseData(String code, String msg) {
this.code = code;
this.msg = msg;
}
public ResponseData(ResultEnums resultEnums) {
this.code = resultEnums.getCode();
this.msg = resultEnums.getMsg();
}
public ResponseData(ResultEnums resultEnums, T data) {
this.code = resultEnums.getCode();
this.msg = resultEnums.getMsg();
this.data = data;
}
public ResponseData() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
ResponseDataUtils
public class ResponseDataUtil {
/**
* 帶實體的統一返回
*
* @param data 實體
* @param <T> 實體型別
* @return
*/
public static <T> ResponseData buildSuccess(T data) {
return new ResponseData<T>(ResultEnums.SUCCESS, data);
}
public static ResponseData buildSuccess() {
return new ResponseData(ResultEnums.SUCCESS);
}
public static ResponseData buildSuccess(String msg) {
return new ResponseData(ResultEnums.SUCCESS.getCode(), msg);
}
public static ResponseData buildSuccess(String code, String msg) {
return new ResponseData(code, msg);
}
public static <T> ResponseData buildSuccess(String code, String msg, T data) {
return new ResponseData<T>(code, msg, data);
}
public static ResponseData buildSuccess(ResultEnums resultEnums) {
return new ResponseData(resultEnums);
}
public static <T> ResponseData buildError(T data) {
return new ResponseData<T>(ResultEnums.ERROR, data);
}
public static ResponseData buildError() {
return new ResponseData(ResultEnums.ERROR);
}
public static ResponseData buildError(String msg) {
return new ResponseData(ResultEnums.ERROR.getCode(), msg);
}
public static ResponseData buildError(String code, String msg) {
return new ResponseData(code, msg);
}
public static <T> ResponseData buildError(String code, String msg, T data) {
return new ResponseData<T>(code, msg, data);
}
public static ResponseData buildError(ResultEnums resultEnums) {
return new ResponseData(resultEnums);
}
}
ResultEnum
public enum ResultEnums {
SUCCESS("0000", "請求成功"),
ERROR("1111", "請求失敗"),
SYSTEM_ERROR("1000", "系統異常"),
BUSSINESS_ERROR("2001", "業務邏輯錯誤"),
VERIFY_CODE_ERROR("2002", "業務引數錯誤"),
PARAM_ERROR("2002", "業務引數錯誤");
private String code;
private String msg;
ResultEnums(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
感興趣得小夥伴,快去實現一下吧。明天見。
相關文章
- 統一controller返回物件Controller物件
- SpringBoot在controller返回一個HTML頁面Spring BootControllerHTML
- springboot請求controller返回Whitelabel Error PageSpring BootControllerError
- springboot返回結果包裝統一返回格式Spring Boot
- go fiber: controller返回json格式GoControllerJSON
- 使用 ResponseBodyAdvice 攔截Controller方法預設返回引數,統一處理返回值/響應體Controller
- springMVC中controller的返回值SpringMVCController
- SpringBoot介面 - 如何優雅的寫Controller並統一異常處理?Spring BootController
- SpringBoot(三)_controller的使用Spring BootController
- k8s自定義controller三部曲之三:編寫controller程式碼K8SController
- springboot使controller非同步呼叫Spring BootController非同步
- springboot2.0-統一處理返回結果和異常情況Spring Boot
- Springboot使用ResponseBodyAdvice進行統一返回介面資料格式的使用Spring Boot
- springboot統一異常處理及返回資料的處理Spring Boot
- springboot中controller返回實體類中過濾掉等於null或為空的欄位Spring BootControllerNull
- k8s自定義controller三部曲之一:建立CRD(Custom Resource Definition)K8SController
- SpringBoot 如何統一後端返回格式?老鳥們都是這樣玩的!Spring Boot後端
- SpringBoot入門教程(十七)@Service、@Controller、@Repository、@ComponentSpring BootController
- AngularJS之Scope及ControllerAngularJSController
- kubernetes實踐之四十八:Service Controller與Endpoint ControllerController
- 每天用SpringBoot,還不懂RESTful API返回統一資料格式是怎麼實現的?Spring BootRESTAPI
- SpringBoot原始碼解析-controller層引數的封裝Spring Boot原始碼Controller封裝
- SpringBoot Controller介面接收資料(二)(適合萌新)Spring BootController
- SpringBoot 系列 web 篇之自定義返回 Http Code 的 n 種姿勢Spring BootWebHTTP
- k8s自定義controller三部曲之二:自動生成程式碼K8SController
- Linux晶片驅動之SPI ControllerLinux晶片Controller
- Mac系統如何一鍵返回桌面?Mac
- 全域性統一返回結果類
- java統一返回標準型別Java型別
- 統一返回物件和異常處理(一)物件
- SpringBoot2.1版本的個人應用開發框架 - 實現基礎controller、service、前端封裝返回json體等Spring Boot框架Controller前端封裝JSON
- springboot redisTemplate.opsForValue().setIfAbsent返回null原理Spring BootRedisNull
- Springboot專案啟動後訪問Controller報錯404Spring BootController
- RxJava2 + Retrofit2 完全指南 之 對返回Response的統一處理RxJava
- Solon 統一的返回結果調整
- 方法返回資料統一封裝封裝
- Kubernetes安裝之七:配置master之controller-managerASTController
- 編寫一個kubernetes controllerController