springboot异常处理(返回固定格式异常信息)

要求输出的异常信息格式为:

{

"result_code":10000,

"result_msg":"参数不符合要求",

"code":400

}


建公共异常信息类APIException,并继承RuntimeException,不是继承Exception

package com.example.demo.libs.exception;

import lombok.Data;


@Data

public classAPIExceptionextendsRuntimeException{

    private intresult_code=500;

    privateStringresult_msg="异常信息";

    private intcode=500;

    publicAPIException() {

    }

    publicAPIException(Exception_code  exception_code) {

        super(exception_code.getResult_msg());

        this.result_code=exception_code.getResult_code();

        this.result_msg=exception_code.getResult_msg();

       this.code=exception_code.getCode();

    }

    @Override

    public synchronized Throwable  fillInStackTrace() {

        return this;

    }

 }

建具体的异常信息枚举类(枚举对象一定要先放在前面):

package com.example.demo.libs.exception;


public enum Exception_code{

    SUCCESS(200,"成功!",200),

    NOT_FOUND(404,"请求路径有误",404),


    private int  result_code;

    private String  result_msg;

    private int  code;

    public int  getResult_code() {

        return  result_code;

    }

    public  String  getResult_msg() {

        return  result_msg;

    }

    public int  getCode() {

        return  code;

    }

    Exception_code(int  result_code,String  result_msg,int  code) {

        this.result_code=result_code;

        this.result_msg=result_msg;

        this.code=code;

    }

}

建ExceptionBody类来规定reponse中的异常信息输出格式:

package com.example.demo.libs.exception;

import com.alibaba.fastjson.JSONObject;

import lombok.Data;


@Data

public class ExceptionBody{

    private int  result_code;

    private  String  result_msg;

    private int  code;

    public  ExceptionBody(APIExceptionexception) {

        this.result_code=exception.getResult_code();

        this.result_msg=exception.getResult_msg();

        this.code=exception.getCode();

    }

    public static  ExceptionBody  error(APIException  exception) {

        ExceptionBody  e=new  ExceptionBody(exception);

        return e;

    }

    @Override

    public  String  toString() {

        return  JSONObject.toJSONString(this);

    }

}

建GlobalExceptionHandler类来捕捉抛出的异常,并统一处理返回到reponse:

package  com.example.demo.libs.exception;

import  org.slf4j.Logger;//注意是org.slf4j.Logger

import  org.slf4j.LoggerFactory;////注意是org.slf4j.LoggerFactory

import  org.springframework.web.bind.annotation.ControllerAdvice;

import  org.springframework.web.bind.annotation.ExceptionHandler;

import  org.springframework.web.bind.annotation.ResponseBody;


@Controller  Advice

public class GlobalExceptionHandler{

    private static final  Loggerlogger=LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value=APIException.class)//捕捉异常

    @ResponseBody  //将信息写入Response

    publicExceptionBodyexceptionHandler(APIExceptione){

        logger.error("发生异常!原因是:",e.getResult_msg());

        returnExceptionBody.error(e);

    }

}

建controller类进行测试:

@RequestMapping(value="/add5",method=RequestMethod.GET)

public JSONObject  add5(){

    JSONObject  result=new  JSONObject();

    int  i=1;

    if(i==1){

    throw new  APIException(Exception_code.NOT_FOUND);

    }

    return  result;

    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容