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相比之前版本少 ...
随机推荐
- the nearest point/vertex point of linestring
引用https://github.com/Toblerity/Shapely/issues/190 snorfalorpagus commented on 18 Oct 2014 The point ...
- RESTful规范与django编写restful接口
一.什么是RESTful规范 ①REST与技术无关,代表的是一种软件架构风格,REST是Representational State Transfer的简称,中文翻译为“表征状态转移” ②REST从资 ...
- Linux 环境安装运行Sqlmap
1.官网下载 .tar.gz 文件 官网地址:http://sqlmap.org/ 2.登录访问linux环境,将压缩包放入/usr/local 路径. 3.在该路径下通过 tar -xzvf f ...
- 分布式-信息方式-ActiveMQ的Message dispatch高级特性之(指针) Message cursors
Message dispatch高级特性之 Message cursors概述 ActiveMQ发送持久消息的典型处现方式是:当消息的消费者准备就绪时,消息发送系统把存储的 消息 ...
- linux 系统的 cache 过大,解决方案
linux buff/cache过大,清理脚本 2018年06月20日 13:44:53 taozhe666 阅读数:6500 三条指令: sync echo 1 > /proc/sys/v ...
- vue网址路由的实时检测
有些时候,我们需要实时的检测网址,来进行判断,操作,处理等等 我们需要使用 watch 的监视器,然后直接进行操作 我们需要 ’$route.path‘ 属性来进行监听,且需要加引号,然后只要页面 ...
- BytesWritable 长度问题(多出空格)
在使用 BytesWritable 进行小文件合并时,发现长度与原类容不一致,会多出一些空格 测试代码 @Test public void test() { String str = "aa ...
- ORACLE ASM 日常管理
ASM概述 Automatic Storage Management(ASM)是Oracle数据库10g中一个非常出色的新特性,它以平台无关的方式提供了文件系统.逻辑卷管理器以及软件RAID等服务.A ...
- C# 查看计算机端口使用状态
using System.Net.NetworkInformation; /// <summary> /// 获取第一个可用的端口号 /// </summary> /// &l ...
- 使用SNMP监控服务器运行情况
系统监测的基本概念及分类: a.系统监测的概述: 如何对现有IT架构的整体以及细节运行情况进行科学.系统和高效地监测是目前各企业运维和管理部门一项非常重要的工作内容.随着当前企业IT环境中服务器.应用 ...