SpringBoot项目中的全局异常处理器 Failed to invoke @ExceptionHandler method
文件下载代码
@RequestMapping(value = { "/data/docking/picture/{id}/{empi}" })
public JsonApi picToJSP(@PathVariable String id, @PathVariable("empi") String empi,
@Validated({ BaseEntity.SelectOne.class }) TFileWork0 tFileWork0, HttpServletRequest request,
HttpServletResponse response) {
FileInputStream in;
String name = null;
Yhxd yhxd = new Yhxd();
yhxd.setEmpi(empi);
List<Map<String, Object>> yhxdList = yhxdService.getList(yhxd);
if (yhxdList != null && yhxdList.size() > 0) {
name = yhxdList.get(0).get("YHMC").toString() + "-心电图-";
}
tFileWork0.setId(id);
Map<String, Object> map = tFileWork0Service.getOne(tFileWork0);
if (map == null) {
return new JsonApi(ApiCodeEnum.NOT_FOUND);
} else {
String url = map.get("file_path").toString() + map.get("file_name");
String na = map.get("file_name").toString();
/*设置文件下載名称*/
String filename = name + na;
try {
// 图片读取路径
String imgUrl = "C:/Users/chenyan/" + url;
in = new FileInputStream(imgUrl);
int i = in.available();
byte[] data = new byte[i];
in.read(data);
in.close();
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return new JsonApi(ApiCodeEnum.OK);
}
}
全局异常处理器
package com.data.docking.exception; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import com.data.docking.tools.ApiCodeEnum;
import com.data.docking.tools.JsonApi; /**
* Copyright © 2019 .
*
* @author: ChenYan
* @date: 2019年10月10日
* @description: 全局异常处理器
*/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @ExceptionHandler(Exception.class)
public JsonApi defaultErrorHandler(Exception e) {
e.printStackTrace();
log.error("error msg:{}", e.getMessage());
return new JsonApi(ApiCodeEnum.ERROR).setMsg(e.getMessage());
}
}
在下载文件的时候报错

只需要把
@ExceptionHandler(Exception.class) 改成 @ExceptionHandler(BindException.class)就可以了。
SpringBoot项目中的全局异常处理器 Failed to invoke @ExceptionHandler method的更多相关文章
- 添加@ControllerAdvice后报错 Failed to invoke @ExceptionHandler method
首先.单独使用ControllerAdvice 无法正常工作.需要配合@EnableWebMvc 使用. @ControllerAdvice @EnableWebMvc pulbic class Ex ...
- SSM之全局异常处理器
1. 异常处理思路 首先来看一下在springmvc中,异常处理的思路: 如上图所示,系统的dao.service.controller出现异常都通过throws Exception向上抛出,最后 ...
- Spring Boot 中全局异常处理器
Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...
- springmvc中拦截器与springmvc全局异常处理器的问题
最近在做一个练手的小项目, 系统架构中用了springmvc的全局异常处理器, 做了系统的统一异常处理. 后来加入了springmvc的拦截器, 为了一些需求, 在拦截器中的 preHandle 方法 ...
- 【spring】-- springboot配置全局异常处理器
一.为什么要使用全局异常处理器? 什么是全局异常处理器? 就是把错误异常统一处理的方法. 应用场景: 1.当你使用jsr303参数校验器,如果参数校验不通过会抛异常,而且无法使用try-catch语句 ...
- 【学习】SpringBoot之全局异常处理器
/** * 全局异常处理器 **/ @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exceptio ...
- SpringBoot项目中遇到的BUG
1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...
- springboot项目中接口入参的简单校验
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- 从源码看全局异常处理器@ExceptionHandler&@ExceptionHandler的生效原理
1.开头在前 日常开发中,几乎我们的项目都会用到异常处理器,我们通常会定制属于自己的异常处理器,来处理项目中大大小小.各种各样的异常.配置异常处理器目前最常用的方式应该是使用@ControllerAd ...
随机推荐
- The 2019 Asia Yinchuan First Round Online Programming F. Moving On
t题目链接:https://nanti.jisuanke.com/t/41290 思路:题目意思很容易想到floyd,但是由于危险度的限制,我们该怎么跑floyd呢. 一开始理解错题目了,以为u-&g ...
- C++网站学习
0.C++ 一个专门做C++的网站 一.以下内容来自LEARN C++ 的<更好编写C++程序的5个建议>部分 1.C++的一些标准: Coding Standards C++ Cor ...
- JUnit 5.x 知识点
出处:https://blinkfox.github.io/2018/11/15/hou-duan/java/dan-yuan-ce-shi-zhi-nan/#toc-heading-14 表面上来看 ...
- CSS Cross-Browser Inline-Block
低版本的IE,火狐 不支持 Inline-Block 属性,想要达到目的我们需要多做一些额外的工作 , 参考页面为:https://blog.mozilla.org/webdev/2009/02/2 ...
- spark延迟调度与动态资源管理
Spark中的延迟调度 Spark的Task的调度过程有五个本地性级别:PROCESS_NODE.NODE_LOCAL.NO_PREF.RACK_LOCAL.ANY.在理想的状态下,我们肯定是想所有的 ...
- codeforces1267G
考虑我们在某个时刻,剩下的数有 $ i $ 个,这些数的和为 $ j $,那么我们期望要抽 $ n \over i $ 次才能取到一个新的物品,这个物品的期望权值为 $ j \over i $,我们花 ...
- 硬币游戏1——打表&&记忆化搜索
题目 Alice和Bo在玩这样一个游戏,给定 $k$ 个数字 $a_1, a_2,..,a_k$.一开始有 $x$ 枚硬币,Alice和Bob轮流取硬币.每次所取的硬币的枚数一定要在 $k$ 个数当中 ...
- Mophues HDU - 4746 (莫比乌斯反演)
Mophues \[ Time Limit: 10000 ms\quad Memory Limit: 262144 kB \] 题意 求出满足 \(gcd\left(a,b\right) = k\), ...
- class-transformer 方便的json 对象转class
在我们日常web开发中经常碰到需要json 到类的处理,因为json 就是普通的数据,没有处理能力,class 具有业务处理能力, 但是需要数据的支持,class-transformer 刚好做为了一 ...
- nightwatch 基于Webdriver的端到端自动化测试框架
nightwatch 是使用nodejs编写的,基于Webdriver api 的端到端自动化测试框架 包含以下特性 清晰的语法,基于js 以及css 还有xpath 的选择器 内置测试runner, ...