springboot2.0处理自定义异常始终返回json
1. 编写自定义异常类
package cn.jfjb.crud.exception;
/**
* @author john
* @date 2019/11/24 - 9:48
*/
public class UserNotExistException extends RuntimeException {
public UserNotExistException() {
super("用户不存在");
}
}
2. 处理自测试异常
package cn.jfjb.crud.handler;
import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author john
* @date 2019/11/24 - 10:43
*/
@ControllerAdvice
public class MyExceptionHandler {
@ResponseBody
@ExceptionHandler(UserNotExistException.class)
public Map<String, Object> handleException(Exception e) {
Map<String, Object> map = new HashMap<>();
map.put("code", "user.notexist");
map.put("message", e.getMessage());
return map;
}
}
3. 配置application.yml文件(不配置的话无法获取exception)
server:
error:
include-exception: true
4. 编写测试
package cn.jfjb.crud.controller;
import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author john
* @date 2019/11/22 - 19:38
*/
@Controller
public class HelloController {
@RequestMapping({"/testException"})
public String testException(@RequestParam("user") String user) {
if (user != "aaa") {
throw new UserNotExistException();
}
return "index";
}
}


springboot2.0处理自定义异常始终返回json的更多相关文章
- springboot2.0处理任何异常返回通用数据格式
异常分为以下三种 自定义异常 可预知异常 不可预知异常 下面具体说明如何分类处理,从而保证无论触发什么异常均可返回理想的自定义数据格式 ResultCode /** * Created by mrt ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- SpringMVC4.0以后版本返回json格式数据问题
第一次写博文写的不好,但希望能帮助大家,有什么偏颇的地方希望大家多多斧正.在这个问题上困扰了我两天,这两天翻来覆去睡不着.一直在想这个问题.废话不多说下面进入正题. 1.创建创建web项目,加入Spr ...
- Jmeter 4.0 对返回Json处理
研究这个的目的是:如果返回信息是比较复杂的,需要对返回信息中数据以及数据的层级做断言 我找了一些我们的接口,层级关系都太简单了,最复杂的就是这两个了:[而且都是get接口,原来get接口还要增加测试点 ...
- SpringBoot2 使用@RestController 无法返回一个地址,前台无法渲染为页面,只能返回json数据
spring boot之访问静态页面 楼主前两天自学spring boot,然后在学习的过程中,出现一个疑问,就是如何去访问静态的html网页,这个问题,楼主上网上搜了下,找到的是在resource目 ...
- 零基础快速入门SpringBoot2.0教程 (二)
一.SpringBoot2.x使用Dev-tool热部署 简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用 官方地址:https://docs.spring. ...
- SpringBoot2.0 基础案例(03):配置系统全局异常映射处理
一.异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常. 1.业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性. 常见的业务 ...
- (六)SpringBoot2.0基础篇- Redis整合(JedisCluster集群连接)
一.环境 Redis:4.0.9 SpringBoot:2.0.1 Redis安装:Linux(Redhat)安装Redis 二.SpringBoot整合Redis 1.项目基本搭建: 我们基于(五) ...
- SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权
新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少 ...
随机推荐
- mysql 报错从 新安装
卸载从新安装,综合运用 https://www.jb51.net/article/146050.htm https://www.jb51.net/article/90275.htm https://w ...
- C语言 - strcmp和strncmp的编程实现及总结
一.strcmp和strncmp的编程实现及总结 1.strcmp函数的实现 要求: 原型: int strcmp(char *dest,char * src,int n); 头文件:# ...
- 13.Python字符串详解(包含长字符串和原始字符串)
简单地理解,字符串就是“一串字符”,也就是用引号包裹的任何数据,比如“Hello,Charlie”是一个字符串,“12345”也是一个字符串. Python 要求,字符串必须使用引号括起来,可以使用单 ...
- pip & conda 换源
conda换源方法具体参考清华大学镜像站Anaconda 镜像使用帮助 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn ...
- /static/fonts/SIMYOU.TTF’ 字体
/** * pdf 加水印 * * @return */ public byte[] pdfAddWaterMark(byte[] byes) { String fileName = UUID.ran ...
- C++入门经典-例3.3-if-else语句的奇偶性判别
1:代码如下: // 3.3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- Python遍历列表删除多个元素或者重复元素
在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if i ...
- C# NAudio 变声
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- gromacs2018使用踩坑记--insert-molecules
1] gmx插入分子[ -f [<.gro / .g96 / ...>] ] [ -ci [<.gro / .g96 / ...>] ] [ -ip [<.dat> ...
- leetcode 215 第K个最大的元素
此问题可转化为Top K问题进行考虑,当用小顶堆选出K个最大值时,堆顶的元素即为第k大的元素 class Solution { public: int findKthLargest(vector< ...