SpringBoot(十九)_404返回统一异常处理结果
之前写过一篇统一异常处理的文章,今天测试了下如果访问一个不存在的接口,也想返回统一的错误信息,应该怎么做
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返回统一异常处理结果的更多相关文章
- springboot(十九)使用actuator监控应用【转】【补】
springboot(十九)使用actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的 ...
- springboot中web应用的统一异常处理
在web应用中,请求处理过程中发生异常是非常常见的情况.springboot为我们提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异 ...
- SpringBoot第十四篇:统一异常处理
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10984081.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 本文将谈论 ...
- FastAPI 学习之路(五十九)封装统一的json返回处理工具
这之前的接口,我们返回的格式都是每个接口异常返回的数据格式都会不一样,我们处理起来没有那么方便,我们可以封装一个统一的json处理. 那么我们看下如何来实现呢 from fastapi import ...
- springboot(十九):使用Spring Boot Actuator监控应用
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...
- springboot(十九)使用actuator监控应用
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...
- SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用
现在在的公司用spring.profiles.active=@profiles.active@ 当我看到这个的时候,一脸蒙蔽,这个@ 是啥意思. 这里其实是配合 maven profile进行选择不同 ...
- springboot(十九)-线程池的使用
我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行. 话不多说,编码开始: 1.创建spri ...
- springboot(十九):SpringBoot+EHcache实现缓存
https://blog.csdn.net/qq_28143647/article/details/79789368
随机推荐
- 简述WPF中的画刷(Brush)
原文:简述WPF中的画刷(Brush) -------------------------------------------------------------------------------- ...
- UVA10940 - Throwing cards away II(找到规律)
UVA10940 - Throwing cards away II(找规律) 题目链接 题目大意:桌上有n张牌,依照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.重复进行 ...
- WPF控件的一些特殊应用
1 checkbox.IsChecked 返回的是bool?类型,需要用bool强转,或者直接和bool类型比较,将发生隐形转换 2 RadioButton有分组属性GroupName
- wpf 事件参数 绑定到viewmdoel
public sealed class EventCommand : TriggerAction<DependencyObject> { public static readonly De ...
- dataGrid 源更新 事件
DataGrid myGrid = new DataGrid(); CollectionView myCollectionView = (CollectionView)CollectionViewSo ...
- Delphi7程序调用C#写的DLL解决办法
近来,因工作需要,必须解决Delphi7写的主程序调用C#写的dll的问题.在网上一番搜索,又经过种种试验,最终证明有以下两种方法可行: 编写C#dll的方法都一样,首先在vs2005中创建一 ...
- js操作select控件大全(包含新增、修改、删除、选中、清空、判断存在等)
原文:js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js操作select控件大全(包含新增.修改.删除.选中.清空.判断存在等) js 代码// 1.判断select选 ...
- 微信小程序把玩(二十八)image组件
原文:微信小程序把玩(二十八)image组件 image组件也是一个程序不可缺少的,可以这样说一个app中image组件随处可以看到,一般 image有两种加载方式第一种是网络图片第二种是本地图片资源 ...
- sql语句查询重复值
select * from user where name in (select name from user group by name having count(*)>1)
- kettle设计器连接oracle RAC时的连接字符串
1.不需要填写主机名2.数据库名写如下连接字符串:(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = IP地址)(PORT = 1521))(CONNEC ...