spring boot 自定义异常
1.创建一个异常:
public class LdapQueryException extends Exception {
private Integer code;
private String message; public LdapQueryException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} @Override
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
2.写一个异常处理器 适应注解 @ControllerAdvice 植入
@ControllerAdvice
public class CommonExceptionHandler {
@ResponseBody
@ExceptionHandler(value = LdapQueryException.class) //value 表示处理哪个异常
public Map ldapQueryHandler(LdapQueryException exp){
Map map=new HashMap();
map.put("code",exp.getCode());
map.put("message",exp.getMessage());;
return map;
}
}
3.使用: throw new LdapQueryException(500, "系统错误")
spring boot 自定义异常的更多相关文章
- Spring Boot与Logback的运用(自定义异常+AOP)
在开发以及调试过程中,程序员对日志的需求是非常大的,出了什么问题,都要通过日志去进行排查,但是如果日志不清或者杂乱无章,则不利于维护 这边就比较详细的列举几种类型的日志,供大家参考 首先明白logba ...
- Spring Boot结和Spring Data(Ehcache缓存,Thymeleaf页面,自定义异常页面跳转,Swagger2)
项目结构 pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring Boot 系列教程6-全局异常处理
@ControllerAdvice源码 package org.springframework.web.bind.annotation; import java.lang.annotation.Ann ...
- spring boot / cloud (二) 规范响应格式以及统一异常处理
spring boot / cloud (二) 规范响应格式以及统一异常处理 前言 为什么规范响应格式? 我认为,采用预先约定好的数据格式,将返回数据(无论是正常的还是异常的)规范起来,有助于提高团队 ...
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
- 基于Spring Boot的RESTful API实践(一)
1. RESTful简述 REST是一种设计风格,是一组约束条件及原则,而遵循REST风格的架构就称为RESTful架构,资源是RESTful的核心,一个好的RESTful架构,通过URL就能很 ...
- Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理
在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder.@ModelAttribute,并应用到所有@Requ ...
- Spring Boot 快速入门笔记
Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
- Spring Boot 中关于自定义异常处理的套路!
在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @ControllerAdvice 来统一处理,也可以自己来定义异常处理方案.Spring Boot 中,对异常的处理有 ...
随机推荐
- test20180922 交错的字符串
题意 分析 这个数据范围容易使人想到折半搜索. 我们将字符串分为前后两部分.如果前半部分中搜得的前缀串为{S1, S2},那么后半部分中搜得的后缀串必须为{rev(S2), rev(S1)},且为有序 ...
- nexage video asset tag
video ad can't show InLine must match the example ,and xml content is Case Sensitive https:/ ...
- Codeforces Round #243 (Div. 2)——Sereja and Swaps
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24665103 题目链接 题意: 给定一个整数 ...
- FastAdmin 提示框 toastr 改变文字
如下图这个消息提示框为 toastr. FastAdmin 用的就是 toast 前端组件. 那如何改变这个文字呢? Karson:只要后台使用$this->success("自定义成 ...
- Spring核心思想:“控制反转”,也叫“依赖注入” 的理解
@Service对应的是业务层Bean,例如: @Service("userService") public class UserServiceImpl implements Us ...
- Spring MVC 向页面传值-Map、Model、ModelMap、ModelAndView
Spring MVC 向页面传值,有4种方式: ModelAndView Map Model ModelMap 使用后面3种方式,都是在方法参数中,指定一个该类型的参数. Model Model 是一 ...
- Netflix 是怎样的一家公司?为什么它在美国非常成功
https://www.zhihu.com/question/19552101 作者:陈达链接:https://www.zhihu.com/question/19552101/answer/11486 ...
- shell教程-001:shell简介 什么是shell,shell命令的两种执行方式
Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的. Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解 ...
- FineUI 修改config表属性
此功能可用来设置系统的不同的标题 private void SelectSystem() { ConfigHelper.Title = DropDownList1.SelectedText; Conf ...
- Go随机数的使用
随机数使用比较广泛,例如,抽奖.均衡等等. 下面简单说明其使用方法. Example1 package main import ( "log" "math/rand&qu ...