SpringBoot 封装通用返回对象,及全局异常配置

一、简介

我们平时的WEB 开发Controller中会经常用到通用返回类,及全局异常的捕获和处理,及自定义业务异常的情况,接下来我给大家梳理一下配置步骤

二、配置步骤

1. 通用返回类 AjaxResult

package com.dechnic.transfer.common;/**
 * @className: AjaxResult
 * @author: houqd
 * @date: 2022/11/2
 **/

import java.io.Serializable;
import java.util.HashMap;

/**
 *@description: 通用返回结果类
 *@author:houqd
 *@time: 2022/11/2 14:26
 *
 */

public class AjaxResult extends HashMap<String,Object> implements Serializable {
    private static final String REQ_TAG = "success";//请求状态
    /*状态码*/
    private static final String CODE_TAG = "code";
    /*返回内容*/
    private static final String MSG_TAG = "message";
    /*数据对象*/
    private static final String DATA_TAG = "data";

    public AjaxResult (boolean success,String code,String msg,Object data){
        super.put(REQ_TAG,success);
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
        super.put(DATA_TAG, data);
    }

    public AjaxResult (String code,String msg,Object data){
        super.put(REQ_TAG,true);
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
        super.put(DATA_TAG, data);
    }


    public AjaxResult (String code,String msg){
        super.put(REQ_TAG,true);
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
    }

    public AjaxResult() {
    }

    public static AjaxResult success(String msg,Object data){

        return new AjaxResult(StatusCode.SUCCESS.getCode(), msg,data);
    }
    public static AjaxResult success(Object data){

        return new AjaxResult(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getDesc(),data);
    }
    public static AjaxResult error(boolean success,String code ,String msg){
        return new AjaxResult(success,code, msg,null);
    }
    public static AjaxResult error(String code ,String msg){
        return new AjaxResult(code, msg,null);
    }
    public static AjaxResult error(String msg){
        return new AjaxResult(StatusCode.ERROR.getCode(), msg,null);
    }

    public static AjaxResult error(){
        return new AjaxResult(StatusCode.ERROR.getCode(), StatusCode.ERROR.getDesc(),null);
    }

    public static AjaxResult serviceInnerError(){
        return new AjaxResult(StatusCode.SERVICE_ERROR.getCode(), StatusCode.SERVICE_ERROR.getDesc(),null);
    }

    @Override
    public Object put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}

2.StatusCode 状态码配置

package com.dechnic.transfer.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

/**
 * @className: StatusCode
 * @author: houqd
 * @date: 2022/11/2
 **/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum StatusCode {
    SUCCESS("1","成功"),ERROR("0","失败"),NO_LOGIN("401","未登录"),SERVICE_ERROR("500","服务器错误");
    private String code;
    private String desc;
}

3. 全局异常配置

GlobalExceptionHandler

package com.dechnic.transfer.exception;/**
 * @className: GlobalExceptionHandler
 * @author: houqd
 * @date: 2022/11/2
 **/

import com.dechnic.transfer.common.AjaxResult;
import com.dechnic.transfer.common.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 *@description:
 *@author:houqd
 *@time: 2022/11/2 15:34
 *
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(NotLoginException.class)
    public AjaxResult handlerNotLoginException(NotLoginException e, HttpServletRequest request){
        String requestURI =request.getRequestURI();
        log.info("请求地址:'{}',未登陆",requestURI);
        return AjaxResult.error(false,e.getCode(),e.getMsg());
    }

    @ExceptionHandler(BusinessException.class)
    public AjaxResult handlerBusinessException(BusinessException e, HttpServletRequest request){
        String requestURI =request.getRequestURI();
        log.info("请求地址:'{}',业务异常:{}",requestURI,e.getMessage());
        return AjaxResult.error(false,e.getCode(),e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public AjaxResult handlerBusinessException(Exception e, HttpServletRequest request){
        String requestURI =request.getRequestURI();
        log.info("请求地址:'{}',服务器内部异常:{}",requestURI,e.getMessage());
        e.printStackTrace();
        return AjaxResult.error(false,StatusCode.SERVICE_ERROR.getCode(), StatusCode.SERVICE_ERROR.getDesc());
    }

}

BusinessException 业务异常

package com.dechnic.transfer.exception;/**
 * @className: BusinessException
 * @author: houqd
 * @date: 2022/11/2
 **/

import lombok.Data;

/**
 *@description:
 *@author:houqd
 *@time: 2022/11/2 15:20
 *
 */
@Data
public class BusinessException extends RuntimeException{
    private String code;
    private String msg;

    public BusinessException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return msg;
    }
}

NotLoginException

package com.dechnic.transfer.exception;/**
 * @className: NotLoginException
 * @author: houqd
 * @date: 2022/11/2
 **/

import com.dechnic.transfer.common.StatusCode;

/**
 *@description:
 *@author:houqd
 *@time: 2022/11/2 15:37
 *
 */

public class NotLoginException extends BusinessException{
    public NotLoginException() {
        super(StatusCode.NO_LOGIN.getCode(), StatusCode.NO_LOGIN.getDesc());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值