package com.example.forum.exception;
import com.example.forum.dto.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 全局异常捕获
*/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
//错误显示页面
public static final String viewName = "common/error/error";
/**
* 是否是ajax请求
*/
public static boolean isAjax(HttpServletRequest httpRequest) {
return (httpRequest.getHeader("X-Requested-With") != null
&& "XMLHttpRequest"
.equals(httpRequest.getHeader("X-Requested-With").toString()));
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleMissingServletRequestParameterException(MissingServletRequestParameterException e, Model model) {
log.error("缺少请求参数", e);
String message = "【缺少请求参数】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public String handleHttpMessageNotReadableException(HttpMessageNotReadableException e, Model model) {
log.error("参数解析失败", e);
String message = "【参数解析失败】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e, Model model) {
log.error("参数验证失败", e);
BindingResult result = e.getBindingResult();
FieldError error = result.getFieldError();
String field = error.getField();
String code = error.getDefaultMessage();
String message = "【参数验证失败】" + String.format("%s:%s", field, code);
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public String handleBindException(BindException e, Model model) {
log.error("参数绑定失败", e);
BindingResult result = e.getBindingResult();
FieldError error = result.getFieldError();
String field = error.getField();
String code = error.getDefaultMessage();
String message = "【参数绑定失败】" + String.format("%s:%s", field, code);
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public String handleServiceException(ConstraintViolationException e, Model model) {
log.error("参数验证失败", e);
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
ConstraintViolation<?> violation = violations.iterator().next();
String message = "【参数验证失败】" + violation.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 400 - Bad Request
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public String handleValidationException(ValidationException e, Model model) {
log.error("参数验证失败", e);
String message = "【参数验证失败】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 400);
return viewName;
}
/**
* 404 - Not Found
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public String noHandlerFoundException(NoHandlerFoundException e, Model model) {
log.error("Not Found", e);
String message = "【页面不存在】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 404);
return viewName;
}
/**
* 405 - Method Not Allowed
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public String handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, Model model) {
log.error("不支持当前请求方法", e);
String message = "【不支持当前请求方法】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 405);
return viewName;
}
/**
* 415 - Unsupported Media Type
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public String handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e, Model model) {
log.error("不支持当前媒体类型", e);
String message = "【不支持当前媒体类型】" + e.getMessage();
model.addAttribute("message", message);
model.addAttribute("code", 415);
return viewName;
}
/**
* 统一异常处理
*
* @param response
* @param e
* @return
*/
@ExceptionHandler(MyBusinessException.class)
@ResponseBody
public JsonResult processApiException(HttpServletResponse response,
MyBusinessException e) {
JsonResult result = new JsonResult(0, e.getMessage());
response.setStatus(200);
response.setContentType("application/json;charset=UTF-8");
log.error("业务异常,提示前端操作不合法", e.getMessage(), e);
return result;
}
/**
* 获取其它异常。包括500
*
* @param e
* @return
* @throws Exception
*/
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request,
HttpServletResponse response,
Exception e, Model model) throws IOException {
e.printStackTrace();
if
基于springboot的动漫弹幕网站+源代码+文档说明


2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
## 项目说明 基于 springboot+mybatis+thymeleaf实现的动漫网站。<br/> 优势:能发送弹幕、推荐使用协同过滤 ## 功能介绍 - 包含游客、用户和管理员三种角色 - 游客可以只能看视频 - 用户可以发布视频、发送弹幕、留言、点赞等 - 管理员可以审核视频,视频管理、分类管理、标签管理、留言管理、用户管理等等 ## 项目截图                  ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。































































































































- 1
- 2
- 3
- 4
- 5

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 1534
- 资源: 3286





我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 银河麒麟系统ARM架构终端奇安信浏览器deb安装包
- python+pygame+贪食蛇
- vulkan-devel-1.1.97.0-1.el7.x64-86.rpm.tar.gz
- PyTorch入门:从张量到神经网络基础教程
- apache-ivy-2.3.0-4.el7.x64-86.rpm.tar.gz
- 消息中间件常见面试题汇总
- 机器学习-决策树-通过标注数据进行情景推荐
- HCIE-Data Center Network V1.0 培训教材
- 北京理工大学+数据结构(教学课件)
- jaxlib-0.4.17-cp312-cp312-macosx_11_0_arm64.whl
- jaxlib-0.4.17-cp311-cp311-macosx_11_0_arm64.whl
- RocketMQ5安装与集群搭建
- jaxlib-0.4.17-cp312-cp312-macosx_10_14_x86_64.whl
- 斯坦福大学机器学习课程个人学习笔记(上)
- 图像识别-基于CNN模型的验证码高精度识别
- jaxlib-0.4.17-cp39-cp39-macosx_11_0_arm64.whl


