当前位置:   article > 正文

SpringBoot 封装通用返回对象,及全局异常配置_springboot中提供的返回对象

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

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;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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());
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

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;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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());
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/183989
推荐阅读
相关标签
  

闽ICP备14008679号