1. 统一格式返回实体类
package com.yangxz.response; import lombok.Data; import java.util.Map; /** * 统一返回类 */ @Data public class R { /** * 响应是否成功 */ private Boolean success; /** * 响应状态码 */ private Integer code; /** * 响应信息 */ private String message; /** * 返回数据 */ private Object data; /** * 私有构造器 */ private R() { } /** * 通用返回成功 */ public static R ok() { R r = new R(); r.setSuccess(RCondeEnum.SUCCESS.getSuccess()); r.setCode(RCondeEnum.SUCCESS.getCode()); r.setMessage(RCondeEnum.SUCCESS.getMessage()); return r; } /** * 通用返回失败,未知错误 */ public static R error() { R r = new R(); r.setSuccess(RCondeEnum.ERROR_UNKNOWN.getSuccess()); r.setCode(RCondeEnum.ERROR_UNKNOWN.getCode()); r.setMessage(RCondeEnum.ERROR_UNKNOWN.getMessage()); return r; } /** * 设置结果 */ public static R codeEnum(RCondeEnum result) { R r = new R(); r.setSuccess(result.getSuccess()); r.setCode(result.getCode()); r.setMessage(result.getMessage()); return r; } /*--------------- 使用链式编程,返回类本身 ---------------*/ /** * 自定义返回结果 */ public R success(Boolean success) { this.setSuccess(success); return this; } /** * 自定义状态码 */ public R code(Integer code) { this.setCode(code); return this; } /** * 自定义信息 */ public R message(String message) { this.setMessage(message); return this; } /** * 自定义返回数据 */ public R data(Map<String, Object> map) { this.setData(map); return this; } public R data(Object obj) { this.setData(obj); return this; } }
2. 枚举值
package com.yangxz.response; import lombok.Getter; /** * 结果类枚举 */ @Getter public enum RCondeEnum { SUCCESS(true, 200, "成功"), ERROR_UNKNOWN(false, 404, "未知错误"), ERROR_PARAM(false, 601, "参数错误"); /** * 响应是否成功 */ private Boolean success; /** * 响应状态码 */ private Integer code; /** * 响应信息 */ private String message; RCondeEnum(Boolean success, Integer code, String message) { this.success = success; this.code = code; this.message = message; } }
3. 切面处理
package com.yangxz.response; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; /** * Controller 统一返回 * (basePackages = "com.yangxz.base") 解决Swagger 报Unable to infer base url 的问题 */ @ControllerAdvice(basePackages = "com.yangxz") public class ResultControllerAdvice implements ResponseBodyAdvice<Object> { /** * 1、如果返回类型是BaseResult类型,则返回true。返回true就直接结束了 * 2、如果不是BaseResult类型,会进入到beforeBodyWrite这个方法内部 */ @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { if (body instanceof R) { return body; } if (body instanceof String) { ObjectMapper objectMapper = new ObjectMapper(); try { // 将数据包装在BaseResult里后,再转换为json字符串响应给前端 return objectMapper.writeValueAsString(R.ok().message((String) body)); } catch (JsonProcessingException e) { throw new RuntimeException("返回String类型错误"); } } else { return R.ok().data(body); } } }
4. 测试结果