在使用Springboot
作为工程选型技术的时候,由于其三大特性的自动装配,使得我们把注意力更多的放在功能代码实现上,同时也对代码的编写质量提出了更高的要求,这里所说的质量,说的是编码规范。接下来的我会分为三个步骤去了解整个编码基础构建的要点,Controller
统一返回、Controller
请求AOP日志管理、Controller
统一异常处理。
为什么需要统一返回数据?如果你和你的同时合作过,他来开发后端接口,你来开发前端界面,我想在交互的时候,你会有多么期待统一返回,这会让你的请求变得无比得清秀。下面是一些非统一返回得实例:
@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"}}
前端童鞋可以通过code得约定来处理结果。同样得后端同学也可一写出更加简洁易懂得代码。ResponseData
作为统一返回得实体,由ResponseData
作为容器把消息返回。其结构也超级简单,需要三个组件:
ResponseData
ResponseDataUtils
ResultEnum
下面分别给出其结构:ResponseData
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;
}
}
ResponsDataUtils
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
的统一返回实现很简单,但是作用去很大,喜欢的小伙伴可以试着改造以一下自己的工程哟。明天见。