8. 实战脚手架搭建-前后端分离数据封装 DataResult
目前市面上公司开发模式普遍采用了前后端分离,而前后端交互一般会以 json 的形式交互,既然涉及到多方交互那就需要一些约定好的交互格式,然而每个人的想法有可能是不一样的千人前面所以定义的格式字段就可能不一样,如果我们后端不统一前端会”炸的“ 笑脸~~~ 所以我们需要封装一个统一的返回格式。
8.1 创建 DataResult 类
package com.yingxue.lesson.utils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @ClassName: DataResult
* TODO:类文件简单描述
* @Author: 小霍
* @UpdateUser: 小霍
* @Version: 0.0.1
*/
@Data
public class DataResult <T> {
/**
* 请求响应code,0为成功 其他为失败
*/
@ApiModelProperty(value = "请求响应code,0为成功 其他为失败", name = "code")
private int code = 0;
/**
* 响应异常码详细信息
*/
@ApiModelProperty(value = "响应异常码详细信息", name = "msg")
private String msg;
/**
* 响应内容 , code 0 时为 返回 数据
*/
@ApiModelProperty(value = "需要返回的数据", name = "data")
private T data;
}
8.2 加入相应的构造方法
public DataResult(int code, T data) {
this.code = code;
this.data = data;
this.msg=null;
}
public DataResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public DataResult(int code, String msg) {
this.code = code;
this.msg = msg;
this.data=null;
}
8.3 封装统一的相应 code 工具类
8.3.1 创建 ResponseCodeInterface
package com.yingxue.lesson.exception.code;
/**
* @ClassName: ResponseCodeInterface
* TODO:类文件简单描述
* @Author: 小霍
* @UpdateUser: 小霍
* @Version: 0.0.1
*/
public interface ResponseCodeInterface {
int getCode();
String getMsg();
}
8.3.2 创建 BaseResponseCode 枚举类
package com.yingxue.lesson.exception.code;
/**
* @ClassName: BaseResponseCode
* TODO:类文件简单描述
* @Author: 小霍
* @UpdateUser: 小霍
* @Version: 0.0.1
*/
public enum BaseResponseCode implements ResponseCodeInterface{
/**
* 这个要和前段约定好
*code=0:服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。
*code=4010001:(授权异常) 请求要求身份验证。 客户端需要跳转到登录页面重新登录
*code=4010002:(凭证过期) 客户端请求刷新凭证接口
*code=4030001:没有权限禁止访问
*code=400xxxx:系统主动抛出的业务异常
*code=5000001:系统异常
*
*/
SUCCESS(0,"操作成功")
;
/**
* 错误码
*/
private final int code;
/**
* 错误消息
*/
private final String msg;
BaseResponseCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public int getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
}
8.4 进一步封装 DataResult
为了 DataResult 可以提供给我们更友好的服务,我们对 DataResult 进行封装。
public DataResult() {
this.code=BaseResponseCode.SUCCESS.getCode();
this.msg=BaseResponseCode.SUCCESS.getMsg();
this.data=null;
}
public DataResult(T data) {
this.data = data;
this.code=BaseResponseCode.SUCCESS.getCode();
this.msg=BaseResponseCode.SUCCESS.getMsg();
}
public DataResult(ResponseCodeInterface responseCodeInterface) {
this.data = null;
this.code = responseCodeInterface.getCode();
this.msg = responseCodeInterface.getMsg();
}
public DataResult(ResponseCodeInterface responseCodeInterface, T data) {
this.data = data;
this.code = responseCodeInterface.getCode();
this.msg = responseCodeInterface.getMsg();
}
/**
* 操作成功 data为null
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param
* @return com.xh.lesson.utils.DataResult<T>
* @throws
*/
public static DataResult success(){
return new DataResult();
}
/**
* 操作成功 data 不为null
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param data
* @return com.xh.lesson.utils.DataResult<T>
* @throws
*/
public static <T>DataResult success(T data){
return new DataResult(data);
}
/**
* 自定义 返回操作 data 可控
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param code
* @param msg
* @param data
* @return com.xh.lesson.utils.DataResult
* @throws
*/
public static <T>DataResult getResult(int code,String msg,T data){
return new DataResult(code,msg,data);
}
/**
* 自定义返回 data为null
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param code
* @param msg
* @return com.xh.lesson.utils.DataResult
* @throws
*/
public static DataResult getResult(int code,String msg){
return new DataResult(code,msg);
}
/**
* 自定义返回 入参一般是异常code枚举 data为空
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param responseCode
* @return com.xh.lesson.utils.DataResult
* @throws
*/
public static DataResult getResult(BaseResponseCode responseCode){
return new DataResult(responseCode);
}
/**
* 自定义返回 入参一般是异常code枚举 data 可控
* @Author: 小霍
* @UpdateUser:
* @Version: 0.0.1
* @param responseCode
* @param data
* @return com.xh.lesson.utils.DataResult
* @throws
*/
public static <T>DataResult getResult(BaseResponseCode responseCode, T data){
return new DataResult(responseCode,data);
}
8.5 创建 测试 DataResult 接口
@GetMapping("/home")
@ApiOperation(value = "测试DataResult接口")
public DataResult<String> getHome(){
DataResult<String> result=DataResult.success("哈哈哈哈测试成功 欢迎来到迎学教育");
return result;
}
8.6 解决 spring boot devtool 热部署后出现访问 404 问题
DevTools的检测时间和idea的编译所需时间存在差异。在idea还没完成编译工作前,DevTools就开始进行重启和加载,导致@RequestMapping没有被全部正常处理。其他方法没试,就直接用了看起来最简单的方法:牺牲一点时间,去加长devtools的轮询时间,增大等待时间。
解决方案如下:
spring.devtools.restart.poll-interval=3000ms
spring.devtools.restart.quiet-period=2999ms
8.7 打开 swagger UI 测试验证
- 浏览器输入:http://localhost:8080/swagger-ui.html
- 找到我们创建的 测试接口测试