原始的返回
/**
* 操作消息提醒
*
* @author
*/
@ApiModel(description = "返回数据封装")
public class AjaxResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
@ApiModelProperty("状态码")
public static final String CODE_TAG = "code";
@ApiModelProperty("返回信息")
public static final String MSG_TAG = "msg";
@ApiModelProperty("返回数据")
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult() {}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code
* 状态码
* @param msg
* 返回内容
*/
public AjaxResult(int code, String msg) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code
* 状态码
* @param msg
* 返回内容
* @param data
* 数据对象
*/
public AjaxResult(int code, String msg, Object data) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (StringUtils.isNotNull(data)) {
super.put(DATA_TAG, data);
}
}
/**
* 方便链式调用
*
* @param key
* @param value
* @return
*/
@Override
public AjaxResult put(String key, Object value) {
super.put(key, value);
return this;
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success() {
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data) {
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg
* 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg) {
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg
* 返回内容
* @param data
* 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data) {
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static AjaxResult error() {
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg
* 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg) {
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg
* 返回内容
* @param data
* 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data) {
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code
* 状态码
* @param msg
* 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg) {
return new AjaxResult(code, msg, null);
}
}
缺点:每次都需要修改接口的返回
ResponseBodyAdvice接口
- support,通过supports方法,我们可以选择哪些类,或者哪些方法要对response进行处理,其余的则不处理。
- beforeBdoyWrite,在该方法里,为对response处理的具体代码。
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class ApiResponseAdvice implements WebBindingInitializer, ResponseBodyAdvice<Object> {
private final static Gson gson = new Gson();
private static final Set<String> noSelectedConverterUrls = Sets.newHashSet("/swagger-resources", "/v2/api-docs", "/error");
private static final Class[] annos = {RequestMapping.class, GetMapping.class, PostMapping.class, DeleteMapping.class, PutMapping.class};
@Override
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
AnnotatedElement element = returnType.getAnnotatedElement();
return Arrays.stream(annos).anyMatch(anno -> anno.isAnnotation() && element.isAnnotationPresent(anno));
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof AjaxResult) {
return body;
}
if (body instanceof R) {
return body;
}
Object out = body;
final String path = request.getURI().getPath();
if (noSelectedConverterUrls.stream().noneMatch(path::startsWith)) {
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
if (body instanceof ServiceException) {
ServiceException ex = (ServiceException)body;
out = AjaxResult.error(ex.getCode(), ex.getMessage());
} else if (body instanceof Exception) {
out = AjaxResult.error(HttpStatus.ERROR, null);
} else if (body instanceof Throwable) {
out = AjaxResult.error(HttpStatus.ERROR, null);
} else if (body instanceof String) {
out = gson.toJson(AjaxResult.success(body));
} else {
out = AjaxResult.success(body);
}
}
return out;
}
}
添加统一处理之后,Controller上就不需要每一个接口添加ajxaResult类了