之前写过一篇统一异常处理的文章,今天测试了下如果访问一个不存在的接口,也想返回统一的错误信息,应该怎么做

1.修改application.properties文件

# 自定义404
#出现错误时, 直接抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
#不要为我们工程中的资源文件建立映射
spring.resources.add-mappings=false

2.添加controller增强处理

if (e instanceof NoHandlerFoundException) {
return ResultUtil.error(ResultEnum.NO_HANDLER_FOUND_ERROR);
}

3.测试

访问 http://localhost:8080/hello1

// 20190705114619
// http://localhost:8080/hello1 {
"code": 404,
"msg": "接口不存在",
"data": null
}

4.完整controller增强处理类

package com.kevin.common.exception;

import com.kevin.common.entity.Result;
import com.kevin.common.enums.ResultEnum;
import com.kevin.common.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException; import java.util.List; /**
* 异常处理器
*
* @author kevin
* @date 2019/7/4 14:46
*/
@RestControllerAdvice
@Slf4j
public class KevinExceptionHandler { @ExceptionHandler(Exception.class)
public Result handleException(Exception e) {
log.error(e.getMessage(), e); if (e instanceof KevinException) {
return ResultUtil.error(e.getMessage());
} else if (e instanceof NoHandlerFoundException) {
return ResultUtil.error(ResultEnum.NO_HANDLER_FOUND_ERROR);
} else if (e instanceof IllegalArgumentException) {
return ResultUtil.error(e.getMessage());
} else if (e instanceof IllegalStateException) {
return ResultUtil.error(e.getMessage());
} else if (e instanceof BindException) {
BindException ex = (BindException) e;
List<ObjectError> allErrors = ex.getAllErrors();
ObjectError error = allErrors.get(0);
String defaultMessage = error.getDefaultMessage();
return ResultUtil.error(defaultMessage);
} else if (e instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
List<ObjectError> errors = ex.getBindingResult().getAllErrors();
String message = errors.get(0).getDefaultMessage();
return ResultUtil.error(message);
} else {
return ResultUtil.error(ResultEnum.UNKNOW_ERROR);
}
}
}

好了玩的开心

最近在整合一个springboot2.X的框架。里面就集成了这块,有兴趣的可以下载下来看看

地址:https://github.com/FunCodingOfWe/kevin-boot

欢迎start

SpringBoot(十九)_404返回统一异常处理结果的更多相关文章

  1. springboot(十九)使用actuator监控应用【转】【补】

    springboot(十九)使用actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的 ...

  2. springboot中web应用的统一异常处理

    在web应用中,请求处理过程中发生异常是非常常见的情况.springboot为我们提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异 ...

  3. SpringBoot第十四篇:统一异常处理

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10984081.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   本文将谈论 ...

  4. FastAPI 学习之路(五十九)封装统一的json返回处理工具

    这之前的接口,我们返回的格式都是每个接口异常返回的数据格式都会不一样,我们处理起来没有那么方便,我们可以封装一个统一的json处理. 那么我们看下如何来实现呢 from fastapi import ...

  5. springboot(十九):使用Spring Boot Actuator监控应用

    微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...

  6. springboot(十九)使用actuator监控应用

    微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...

  7. SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用

    现在在的公司用spring.profiles.active=@profiles.active@ 当我看到这个的时候,一脸蒙蔽,这个@ 是啥意思. 这里其实是配合 maven profile进行选择不同 ...

  8. springboot(十九)-线程池的使用

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行. 话不多说,编码开始: 1.创建spri ...

  9. springboot(十九):SpringBoot+EHcache实现缓存

    https://blog.csdn.net/qq_28143647/article/details/79789368

随机推荐

  1. Viewport3D中的摄像机(二、摄像机动作)

    原文:Viewport3D中的摄像机(二.摄像机动作) 前文介绍了Viewport3D中的两种摄像机:OrthographicCamera和PerspectiveCamera.在3D场景里漫游,最主要 ...

  2. 使用lead分析功能相似的结构9*9乘法口诀功能

    今天兄弟们的帮助,数据库,具有数据如下面的表: no  name 1    a 2    b 3    c 4    d 怎样用一个sql显演示样例如以下结果: ab ac ad bc bd cd 对 ...

  3. Cocos2d-x 脚本语言Lua介绍

    Cocos2d-x 脚本语言Lua介绍 本篇博客记录Lua学习.学习来自eoe论坛,Lua语言开发Cocos2d-x游戏入门视频教程,猛戳下面地址: http://www.eoeandroid.com ...

  4. windows常用cmd指令

    打开命令行 1.在菜单栏中搜索命令行 2.在文件管理器的Path栏输入cmd,则在当前目录打开命令行 3.Windows+R,输入cmd,回车 ping(网络诊断工具) ping是Windows.Un ...

  5. angular form set dynamic control(form动态设置control)

    实现效果 form表单控件的实时更新 效果如图 关键代码 validateForm: FormGroup; // 表单校验 constructor( private fb: FormBuilder ) ...

  6. 图形化界面安装oracle报错Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPLAY variable is set.

    问题描述: 在Linux + oracle 安装时,采有root 帐号登录x-windows 界面,然后 $su oracle 登录录安装Oracle 报以下错误: >>> Coul ...

  7. SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)

    原文:SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database) 一.本文所涉及的内容(Contents) 本文所涉及的内容 ...

  8. SqlServer 禁止架构更改的复制中手动修复使发布和订阅中分别增加的字段同步

    原文:SqlServer 禁止架构更改的复制中手动修复使发布和订阅中分别增加的字段同步 由于之前的需要,禁止了复制架构更改,以至在发布中添加一个字段,并不会同步到订阅中,而现在又在订阅中添加了一个同名 ...

  9. QDialog之屏蔽Esc键(简单深刻,要么重写keyPressEvent然后break忽略此事件,要么重写eventFilter然后return,都是为了忽略此事件)

    简述 Qt中Esc键会在一些控件中默认的进行一些事件的触发,比如:QDialog,按下Esc键窗口消失.大多数情况下,我们不需要这么做,那么就需要对默认事件进行屏蔽. 简述 源码分析 事件过滤器 事件 ...

  10. storm(二)消息的可靠处理

    storm 通过 trident保证了对消息提供不同的级别.beast effort,at least once, exactly once. 一个tuple 从spout流出,可能会导致大量的tup ...