springboot关于rest api 404、500全局异常处理

两种实现方式

一、通过实现ErrorController接口 重写getErrorPath方法

@RestController
public class RestNotFoundFilter implements ErrorController {

	private static final int NOT_FOUND_CODE = 404;
	
	private static final String ERROR_PATH = "/error";
	
	@RequestMapping(ERROR_PATH)
	@ResponseStatus(value=HttpStatus.NOT_FOUND)
	public Result<?> handleError() {
		Result<?> result = new Result<>();
		result.setErrorcode(NOT_FOUND_CODE);
		result.setErrormsg("访问路径不存在");
		return result;
	}
	
	@Override
	public String getErrorPath() {
		return ERROR_PATH;
	}

}

 返回实体

@Data
public class Result implements Serializable {

    private boolean success;

    private Integer errcode;

    private String errmsg;

    private Object data;

    private Result(boolean success, Integer errcode, String errmsg, Object data){
        this.success = success;
        this.errcode = errcode;
        this.errmsg = errmsg;
        this.data = data;
    }

    /**
     * 有结果集
     * @param success 是否成功
     * @param errcode 返回码
     * @param errmsg 返回信息
     * @param data 返回数据
     * @return
     */
    public static Result ok(boolean success, Integer errcode, String errmsg, Object data){
        return new Result(success,errcode,errmsg,data);
    }

    /**
     * 有返回数据集
     * @param data 返回数据集
     * @return
     */
    public static Result ok(Object data){
       return ok(true,200,null,data);
    }

    /**
     * 无结果集
     * @param errcode 返回码
     * @param errmsg 返回信息
     * @return
     */
    public static Result ok(Integer errcode,String errmsg){
        return ok(true,errcode,errmsg,null);
    }

    /**
     * 无返回数据集
     * @return
     */
    public static Result ok(){
        return ok(true,200,null,null);
    }

    /**
     * 错误
     * @param errcode 错误码
     * @param errmesg 错误信息
     * @return
     */
    public static Result error(Integer errcode,String errmesg){
        return error(errcode,errmesg,null);
    }

    /**
     * 错误
     * @param errcode 错误码
     * @param errmesg 错误信息
     * @param data 错误数据
     * @return
     */
    public static Result error(Integer errcode,String errmesg,Object data){
        return error(false,errcode,errmesg,data);
    }

    /**
     * 错误
     * @param success
     * @param errcode
     * @param errmsg
     * @param data
     * @return
     */
    public static Result error(boolean success, Integer errcode, String errmsg, Object data){
        return new Result(success,errcode,errmsg,data);
    }
}

 二:定义全局异常处理

@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(value=Exception.class)
	public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
		Result<?> result = new Result<>();
		log.info("全局异常处理器执行中...");
		StringBuffer requestURL = request.getRequestURL();
		log.info(">>>>>>>>>>>>>>异常请求路径:"+requestURL+"<<<<<<<<<<<<<<<<<<<<<");
		
		if (e instanceof org.springframework.web.servlet.NoHandlerFoundException) {
			result.setErrorcode(NOT_FOUND_CODE);
       } else {
    	   result.setErrorcode(SERVER_ERROR_CODE);
       }
		return e;
		
	}
}

 并且在配置文件中设置

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

 

第一个 spring.mvc.throw-exception-if-no-handler-found 告诉 SpringBoot 当出现 404 错误时, 直接抛出异常. 第二个 spring.resources.add-mappings 告诉 SpringBoot 不要为我们工程中的资源文件建立映射. 这两个配置正是 RESTful 服务所需要的

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值