这是我参与更文挑战的第4天,活动详情查看: 更文挑战
立个Flag每天写点东西,坚持下去。
老项目改造返回值规范化
背景:
已经运行的项目,开始由于赶工期等因素,未做统一的接口返回规范。现在要做规范化,还必须要保留原先的接口,尤其是APP的接口,有的版本会存在一个比较长的时间。因此需要保留两个版本,又不想维护两套代码。
使用ResponseBodyAdvice 拦截获取controller的response body内容
basePackages 指定需要拦截的包
supports 返回是否需要拦截
通过返回类型、请求路径匹配、header中的参数值等过滤请求内容,然后重写返回对象,返回值。
@Slf4j
@ControllerAdvice(basePackages = "com.paw.response")
public class ApiResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports (MethodParameter methodParameter, Class converterType) {
// 可以过滤拦截哪些内容 比如方法类型
boolean supported = methodParameter.getMethod().getReturnType().equals(Result.class);
return true;
}
@Override
public Object beforeBodyWrite (Object body, MethodParameter methodParameter, MediaType selectedContentType, Class selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {
String path = request.getURI().getPath();
String klassMethod = methodParameter.getMethod().getDeclaringClass().getName() + "#" + methodParameter.getMethod().getName();
log.info("request uri: {} method: {}", path, klassMethod);
if(body instanceof String){
Result result = new Result(200,"success",body);
return JSONUtil.toJsonStr(result);
}
if (!(body instanceof Result)) {
return body;
}
Result result = (Result) body;
// 通过Path匹配,或者header中的值 来过滤请求
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean matched = pathMatcher.match("/api/newUri/**", path);
log.info("path match {} {}", path, matched);
if (matched) {
// 重写返回码 result.setCode(newCode);
}
// 生成新的规范化转码 newCode
// result.setCode(newCode);
return result;
}
}
复制代码
Result.java
@Data
public class Result<T> {
private Integer code;
private String message;
private Object content;
public Result () {
}
public Result (Integer code, String message, Object content) {
this.code = code;
this.message = message;
this.content = content;
}
}
复制代码
测试请求,对 /resCode 请求拦截后重写返回对象,对返回类型为Result重写规范化后的值setCode,setMessage.
@RestController
public class ResController {
@GetMapping("/resCode")
public Object resCode(){
return "this is response data";
}
@GetMapping("/standardResCode")
public Result standardResCode(){
return new Result(200,"Success","standardResCode");
}
}
复制代码
访问 http://localhost:8080/resCode 返回
{"code":200,"message":"success","content":"this is response data"}
复制代码
方式二 通过拦截器Interceptor 或者自定义Aop拦截重设返回值。
项目中一般会为某个业务分配一个值段如810xxx,每个业务定义一个枚举存放返回值code、message.
近期评论