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

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. [Sql Server 2008 基础] With Ties. Over()子句

    with ties WITH TIES 指定从基本结果集中返回额外的行,对于 ORDER BY 列中指定的排序方式参数,这些额外的返回行的该参数值与 TOP n (PERCENT) 行中的最后一行的该 ...

  2. VCL to UniGUI Migration Wizard

    Free Evaluation Edition of The Automatic Migration Scripting Wizard For Converting Legacy Delphi Cod ...

  3. POJ3723 Conscription 【并检查集合】

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2810 Desc ...

  4. android 如何使用jar替换java代码编译

    1. 第一次全编译project(随机eng/user),然后保存,以取代java代码jar包,在alps/out/target/common/obj/JAVA_LIBRARIES/xxx__inte ...

  5. 在实现视频播放器的步骤client(三)风行网络电影列表

    (三) 今日热门电影实现这个功能.主要从server获取数据.然后显示在屏幕上.虽然说是从这个server获取电影信息数据,但,不实际的http相关知识,我们直接sdk包(56网络提供api),你将能 ...

  6. WPF实现弹幕

    实现效果 运用WPF的DoubleAnimation实现桌面端的弹幕效果 示例代码 https://github.com/zLulus/BarrageDemo

  7. WPF UpdateSourceTrigger的使用

    <Window x:Class="XamlTest.Window8"        xmlns="http://schemas.microsoft.com/winf ...

  8. Lexer的设计--上(3)

    lexer的构造函数 有了上一节Token做铺垫, 可以开始设计lexer, 首先应该想到的是, 源代码是以文件流的格式传到编译器中的, 所以作为编译器的前段的第一个阶段, lexer必须负责处理输入 ...

  9. c# 安装windows服务

    C# windows服务: 第一种 :通过cmd命令安装.卸载.启动和停止Windows Service(InstallUtil.exe) 步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行 ...

  10. 动态lambda 构建

    var param = Expression.Parameter(typeof(T)); var datetime1 = Expression.Constant(dt1); var datetime2 ...