@Slf4j
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler({InvalidRequestException.class})
public ResponseEntity<?> handleInvalidRequest(HttpServletRequest request, InvalidRequestException e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(e.getErrorCode().isForm() ? HttpStatus.GONE : HttpStatus.CONFLICT, "logic error", e.getErrorCode() == null ? null : e.getErrorCode().getCode(),
e.getMessage() == null ? ResponseErrorCode.GENERAL_PARAMS_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
} @ExceptionHandler({FormulaErrorException.class})
public ResponseEntity<?> handleFormula(HttpServletRequest request, FormulaErrorException e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE, "logic error", ResponseErrorCode.CONFIG_FORMULA_ERROR.getCode(),
e.getMessage() == null ? ResponseErrorCode.CONFIG_FORMULA_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
}
@ExceptionHandler(value = {AccessDeniedException.class})
public ResponseEntity<?> handlerAccessDeniedException(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) throws IOException {
/* if (Objects.equals(ex.getMessage(), "不允许访问")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
"Forbidden", "no authority", request.getServletPath())));
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.FORBIDDEN,
"Forbidden", "Access Denied", request.getServletPath())));
}*/
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
"Forbidden", "no authority", request.getServletPath())));
} @ExceptionHandler
public ResponseEntity<?> handleException(HttpServletRequest request, Exception e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.CONFLICT, "system error", ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getCode(),
e.getMessage() == null ? ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getDescription() : e.getMessage(), request.getServletPath()), "**"));
} @Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
String error;
for (FieldError fieldError : fieldErrors) {
error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
errors.add(error);
}
for (ObjectError objectError : globalErrors) {
error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
errors.add(error);
}
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE,
"invalidate parameter", ResponseErrorCode.GENERAL_PARAMS_ERROR.getCode(),
StringUtils.join(errors), ((ServletWebRequest) request).getRequest().getServletPath())));
}
}

springboot全局异常处理的更多相关文章

  1. springboot 全局异常处理

    springboot 全局异常处理 研究了半天springboot的全局异常处理,虽然还是需要再多整理一下,但是对于常见的404和500足以可以区分开,能够根据这两个异常分别处理 首先配置视图解析路径 ...

  2. 【第二十三章】 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  3. 第二十三章 springboot + 全局异常处理

    一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...

  4. SpringBoot全局异常处理方式

    每个项目全局异常处理非常重要, 今天在处理项目架构的时候添加了一个全局异常处理. 大概三种异常情况: 一:在进入Controller之前,譬如请求一个不存在的地址,404错误. 二:在执行@Reque ...

  5. SpringBoot全局异常处理与定制404页面

    一.错误处理原理分析 使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状态码一般为500)时,SpringBoot就会给我们返回错 ...

  6. SpringBoot 全局异常处理 @RestControllerAdvice +@ExceptionHandler 请求参数校验

    ControllerAdvice 指示带注释的类辅助“控制器”. 作为的特殊化@Component,允许通过类路径扫描自动检测实现类. 通常用于定义@ExceptionHandler, @InitBi ...

  7. springboot全局异常处理(1)

    新建一个类 在类上加一个注解即可 @ControllerAdvice /** * 全局错误处理 * @author sys * */ @ControllerAdvice @ResponseBody p ...

  8. SpringBoot微服务电商项目开发实战 --- 全局异常处理

    上一篇文章讲了Redis缓存的安全防范及Kafka的接入及消息实现,今天接着前面的内容基础说说项目的优化和基础配置,今天要讲的内容主要是Spring Boot项目中的全局异常处理.为什么要做这件事呢? ...

  9. SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务

    ============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...

随机推荐

  1. ORACLE 监听

    今天来学习一下监听的相关内容,昨晚被老大问了两个关于监听很简单的问题,但是却吞吞吐吐回答,而且有一个问题还答错了,刚刚查了下资料,才发现"驴头对了马嘴",哭笑不得. 一.监听(li ...

  2. web面试题

    1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? Ie(Ie内核) 火狐(Gecko) 谷歌(webkit) opear(Presto)

  3. Beta 集合

    Beta冲刺序列: Beta凡事预则立 :Beta No.0 Beta冲刺Day1:Beta No.1 Beta冲刺Day2:Beta No.2 Beta冲刺Day3:Beta No.3 Beta冲刺 ...

  4. 网络1711班 C语言第七次作业批改总结

    网络1711班 C语言第七次作业批改总结 1.本次作业评分细则 1.1 基本要求(1分) 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 泛泛而谈(最多七分) 1 ...

  5. 201621123031 《Java程序设计》第10周学习总结

    作业10-异常 1.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 1.捕捉异常 Java中的异常捕获结构由try.catch和finally三个部分组成.其中try语句 ...

  6. Python处理图片缩略图

    CPU 密集型任务和 IO 密集型任务分别选择多进程multiprocessing.Pool.map 和多线程库multiprocessing.dummy.Pool.map import os imp ...

  7. bzoj千题计划275:bzoj4817: [Sdoi2017]树点涂色

    http://www.lydsy.com/JudgeOnline/problem.php?id=4817 lct+线段树+dfs序 操作1:access 操作2:u到根的-v到根的-lca到根的*2+ ...

  8. IDEA之Jrebel插件激活

    问题: 码农日常中,热部署是必不可少的,而jrebel插件很好的实现热部署功能. IDEA下载jrebel插件,可以免费试用15天,但之后就无法使用.因为Jrebel是收费的. 解决方法: 楼主也是百 ...

  9. python 编码规范整理

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不要使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号. ...

  10. OAuth2.0学习(3-1)发布 spring-oauth-client 和 spring-oauth-server

    1.发布spring-oauth-server应用 1.1.创建案例数据库oauth2,root/Abc1234% 1.2.执行脚本,创建数据结构和demo数据 init_db.sql (user_用 ...