@RestControllerAdvice("模块根目录包名(防止框架api被封装)")
public class GlobalResponseBodyAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
/**
* @param o
* @param methodParameter
* @param mediaType
* @param aClass
* @param serverHttpRequest
* @param serverHttpResponse
* @return
*/
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
if (o instanceof Result) {
return o;
}
return Result.ok(o);
}
}
API返回值:
@GetMapping
public Result getList() {
return Result.ok("ok");
}
或
@GetMapping
public String getList() {
return "ok";
}