栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot统一接口返回数据的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot统一接口返回数据的实现

一,没有异常的情况,正常返回数据

希望接口统一返回的数据格式如下:

{
  "status": 0,
  "msg": "成功",
  "data": null
}

和接口数据对应的bean


public class Result implements Serializable {

  private static final long serialVersionUID = 1L;

  
  private int status;

  
  private String msg;

  
  private T data;

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }
}

操作Result实体的工具类


public class ResultUtils {

  
  public static  Result success(T t){
    Result result = new Result<>();
    result.setStatus(ResultEnum.SUCCESS.getCode());
    result.setMsg(ResultEnum.SUCCESS.getMsg());
    result.setData(t);
    return result;
  }

  
  public static  Result success(){
    return success(null);
  }

  
  public static  Result error(int status, String msg){
    Result result = new Result<>();
    result.setStatus(status);
    result.setMsg(msg);
    return result;
  }
}

封装错误码和错误消息的枚举类


public enum ResultEnum {

  UNKNOWN_ERROR(-1, "未知错误"),
  SUCCESS(0, "成功"),
  BASIC_INFO_ID_IS_EMPTY(600, "基本信息中BasicInfoId为空"),
  BASIC_INFO_ADD_TO_DATAbase_FAILURE(601, "向数据库添加基本信息失败"),
  DETAILS_DATA_BASIC_INFO_ID_IS_EMPTY(602, "测试数据中BasicInfoId为空"),
  DETAILS_DATA_ADD_TO_DATAbase_FAILURE(603, "向数据库添加测试数据失败");

  ResultEnum(int code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  private int code;

  private String msg;

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "ResultEnum{" +
 "code=" + code +
 ", msg='" + msg + ''' +
 '}';
  }
}

统一封装返回结果的切面

之所以需要这个切面,是为了避免每个Controller方法中都要调用ResultUtils.success()。有了这个切面,Controller可以和原来一样正常返回对象,字符串,void,在切面里面将结果封装成Result实体,而不需要每个Controller方法都返回Result实体。


@ControllerAdvice
public class MyResponseAdvice implements ResponseBodyAdvice {

  @Autowired
  private ObjectMapper objectMapper;

  
  @Override
  public boolean supports(MethodParameter returnType, Class> converterType) {
    return true;
  }

  
  @Override
  public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    if(body instanceof Result){ //发生异常之后,异常处理器里面返回的已经是Result了
      return body;
    }else if(body instanceof String){ //String属于特殊情况,需要单独处理,否则会报错
      try {
 return objectMapper.writevalueAsString(ResultUtils.success(body));
      } catch (JsonProcessingException e) {
 e.printStackTrace();
 return ResultUtils.error(ResultEnum.UNKNOWN_ERROR.getCode(), e.getMessage());
      }
    }
    return ResultUtils.success(body);
  }
}



二,有异常的情况下

    service层为了自动回滚事务,会抛出一些自定义的RuntimeException。默认情况下,只有RuntimeException才会回滚事务。如果Controller里面直接处理service层抛出的异常,则Controller里面到处都是try catch块,代码会很难看。将异常集中在一个地方处理会好很多。

    springboot中是通过@ControllerAdvice和@ExceptionHandler来完成统一异常处理的。这2个注解只能处理Controller和拦截器中抛出的异常,其他地方抛出的异常(比如Filter中抛出的异常),无法捕获。其他地方抛出的异常会转到/error的Controller方法来处理,默认是BasicErrorController来处理,为了能处理其他地方抛出的异常,我们会自定义ErrorController。

统一的异常处理类,处理Controller和拦截器抛出的异常


@ControllerAdvice
public class MyExceptionHandler {

  
  


  
  @ExceptionHandler(BasicInfoException.class)
  @ResponseBody
  public Result handleBasicInfoException(BasicInfoException ex){
    return ResultUtils.error(ex.getCode(), ex.getMessage());
  }

  
  @ExceptionHandler(DetailsDataException.class)
  @ResponseBody
  public Result handleDetailsDataException(DetailsDataException ex){
    return ResultUtils.error(ex.getCode(), ex.getMessage());
  }


  
  @ExceptionHandler(Exception.class)
  @ResponseBody
  public Result handleUnKnowException(Exception ex){
    return ResultUtils.error(ResultEnum.UNKNOWN_ERROR.getCode(), ex.getMessage());
  }

}

自定义的异常类示例

public class BasicInfoException extends RuntimeException {

  private int code;

  public BasicInfoException(int code, String msg){
    super(msg);
    this.code = code;
  }

  public int getCode() {
    return code;
  }
}

处理其他地方抛出的异常(不是Controller和拦截器抛出的异常),自定义ErrorController


@Controller
public class MyBasicErrorController extends AbstractErrorController {

  private Logger logger = LoggerFactory.getLogger(this.getClass());

  
  @Value("${server.error.path}")
  private String myPath;

  private final ErrorProperties errorProperties;

  private ErrorAttributes mErrorAttributes;

  public MyBasicErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
    super(errorAttributes);
    this.errorProperties = serverProperties.getError();
    this.mErrorAttributes = errorAttributes;
  }

  //@RequestMapping(value = "/error")
  @RequestMapping("${server.error.path}") //从properties文件中获取
  @ResponseBody
  public Result error(HttpServletRequest request) throws Throwable {
    logger.debug("myPath = " + myPath);

    //发生错误之后直接将异常抛出去,异常会到统一异常处理器中处理
    WebRequest webRequest = new ServletWebRequest(request);
    Throwable throwable = this.mErrorAttributes.getError(webRequest).getCause();
    throw throwable;
    
    
  }

  
  private boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
    ErrorProperties.IncludeStacktrace include = getErrorProperties().getIncludeStacktrace();
    if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
      return true;
    }
    if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
      return getTraceParameter(request);
    }
    return false;
  }

  
  private ErrorProperties getErrorProperties() {
    return this.errorProperties;
  }


  
  @Override
  public String getErrorPath() {
    return this.errorProperties.getPath();
  }
}

自定义ErrorController中错误处理的方法中,也可以直接将异常抛出,这样异常就会交给统一异常处理器进行处理。

 //@RequestMapping(value = "/error")
  @RequestMapping("${server.error.path}") //从properties文件中获取
  @ResponseBody
  public Result error(HttpServletRequest request) throws Throwable {
    logger.debug("myPath = " + myPath);

    //发生错误之后直接将异常抛出去,异常会到统一异常处理器中处理
    WebRequest webRequest = new ServletWebRequest(request);
    Throwable throwable = this.mErrorAttributes.getError(webRequest).getCause();
    UserException ex;
    if(throwable instanceof UserException){
      ex = (UserException) throwable;
      throw ex;
    }else{
      throw throwable;
    }
    
  }



到此这篇关于springboot统一接口返回数据的实现的文章就介绍到这了,更多相关springboot统一接口返回数据内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号