赞
踩
spring boot 项目在Filter中抛出异常,使用@ControllerAdvice+@ExceptionHandler无法处理,处理方法如下:
自己创建Controller继承BasicErrorController,具体实现
- @RestController
- @Api(value = "filter错误处理", description = "filter错误处理")
- public class ErrorController extends BasicErrorController {
-
- public ErrorController() {
- super(new DefaultErrorAttributes(), new ErrorProperties());
- }
-
- @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
- public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
- Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
- HttpStatus status = getStatus(request);
- //自定义的错误信息类
- //status.value():错误代码,
- //body.get("message").toString()错误信息
- R ret = R.error(status.value(), body.get("message").toString());
- //TokenException Filter抛出的自定义错误类
- if (!Strings.isNullOrEmpty((String) body.get("exception")) && body.get("exception").equals(TokenException.class.getName())) {
- body.put("status", HttpStatus.FORBIDDEN.value());
- status = HttpStatus.FORBIDDEN;
- }
- return new ResponseEntity<Map<String, Object>>(ret, status);
- }
-
- @Override
- public String getErrorPath() {
- return "error/error";
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。