08--SpringBoot之统一化json输出与自定义异常捕获

为及时了解异常,以及避免返回的json格式不统,将每次请求的结果都返还一样的形式。
此处统一为:{“code”:响应代号码,”msg”:信息,”data”:数据} 具体效果如下:

result--format.png

1.格式化请求返回值Bean对象:toly1994.com.toly01.hander.result.ResultEnum
/**
 * 作者:张风捷特烈
 * 时间:2018/5/25:15:30
 * 邮箱:1981462002@qq.com
 * 说明:格式化请求返回值Bean对象
 */
public class ResultBean<T> {
    private int code;
    private String msg;
    private T data;

    //三参构造 set get toString 省略......
}
2.使用枚举类统一错误码与错误信息维护:toly1994.com.toly01.hander.result.ResultEnum
/**
 * 作者:张风捷特烈
 * 时间:2018/5/25:17:36
 * 邮箱:1981462002@qq.com
 * 说明:使用枚举类统一错误码与错误信息维护
 */
public enum ResultEnum {
    SUCCESS(200,"操作成功"),
    EXCEPTION(500, ""),
    NULL_ATTR(101,"属性为空");

    private int code;
    private String msg;

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

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}
3.结果处理类:toly1994.com.toly01.hander.result.ResultHandler
/**
 * 作者:张风捷特烈
 * 时间:2018/5/30:18:37
 * 邮箱:1981462002@qq.com
 * 说明:结果处理类
 */
public class ResultHandler {
    /**
     * 成功时将object对象转化为ResultBean对象
     *
     * @param o
     * @return
     */
    public static ResultBean ok(Object o) {

        return new ResultBean(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), o);
    }

    /**
     * 使用枚举列举错误类型
     *
     * @param error
     * @return
     */
    public static ResultBean error(ResultEnum error) {
        return new ResultBean(error.getCode(), error.getMsg(), null);
    }

    public static ResultBean error(String e) {
        return new ResultBean(ResultEnum.EXCEPTION.getCode(), e, null);
    }
}
4.异常捕获类:toly1994.com.toly01.hander.ExceptionHandle
/**
 * 异常捕获类
 */
@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultBean handle(Exception e) {

        if (e instanceof NullAttrException) {
            return ResultHandler.error(ResultEnum.NULL_ATTR);
        }

        return ResultHandler.error(e.getMessage());
    }
}
5.自定义异常:这里自定义一个演示一下:toly1994.com.toly01.hander.exception.NullAttrException
/**
 * 作者:张风捷特烈
 * 时间:2018/5/25:17:14
 * 邮箱:1981462002@qq.com
 * 说明:属性为空异常
 */
public class NullAttrException extends RuntimeException {
    private int code;
    private static String msg = ResultEnum.NULL_ATTR.getMsg();

    public NullAttrException() {
        super(msg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}
控制器:toly1994.com.toly01.controller.ResultUnitController
/**
 * 作者:张风捷特烈
 * 时间:2018/7/15:21:53
 * 邮箱:1981462002@qq.com
 * 说明:结果返回统一化控制器
 */

@RestController
public class ResultUnitController {
    @Autowired
    private SwordRepository mSwordRepository;

    /**
     * 查询所有:返回json字符串
     *
     * @return
     */
    @GetMapping(value = "/swords/findall")
    public ResultBean findAllToJson() {
        if (1 == 1) {
            throw new NullAttrException();//此处故意抛出自定义异常看看
        }
        return ResultHandler.ok(mSwordRepository.findAll());
    }
}

至此便可完成上图的效果

原文地址:https://www.cnblogs.com/toly-top/p/9781987.html