当前位置: 首页 > news >正文

SpringBoot复习:(37)自定义ErrorController

所有接口统一返回的数据格式

package cn.edu.tju.domain;public class MyResponse {private int code;private String message;private String exception;private String stack;public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getException() {return exception;}public void setException(String exception) {this.exception = exception;}public String getStack() {return stack;}public void setStack(String stack) {this.stack = stack;}
}

自定义的ErrorController覆盖默认的BasicErrorController。当出错时,也统一返回自定义的数据格式,而不是Spring的ResponseEntity

package cn.edu.tju.controller;import cn.edu.tju.domain.MyResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;@Controller
@RequestMapping("/error")
public class MyErrorController extends AbstractErrorController {public MyErrorController(ErrorAttributes errorAttributes, List<ErrorViewResolver> errorViewResolvers) {super(errorAttributes, errorViewResolvers);}@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAaa");HttpStatus status = getStatus(request);ErrorAttributeOptions of = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.EXCEPTION,ErrorAttributeOptions.Include.MESSAGE,ErrorAttributeOptions.Include.EXCEPTION//,ErrorAttributeOptions.Include.STACK_TRACE);Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, of));response.setStatus(status.value());ModelAndView modelAndView = resolveErrorView(request, response, status, model);return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);}@RequestMapping@ResponseBodypublic MyResponse error(HttpServletRequest request) {HttpStatus status = getStatus(request);if (status == HttpStatus.NO_CONTENT) {MyResponse myResponse = new MyResponse();myResponse.setCode(204);return myResponse;}ErrorAttributeOptions of = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.EXCEPTION,ErrorAttributeOptions.Include.MESSAGE,ErrorAttributeOptions.Include.EXCEPTION,ErrorAttributeOptions.Include.STACK_TRACE);Map<String, Object> body = getErrorAttributes(request, of);MyResponse myResponse = new MyResponse();myResponse.setCode(200);myResponse.setMessage(String.valueOf(body.get("message")));myResponse.setException(String.valueOf(body.get("exception")));myResponse.setStack(String.valueOf(body.get("trace")));//path//timestamp//errorreturn myResponse;}
}

SpringBoot默认异常处理自动配置类ErrorMvcAutoConfiguration

http://www.lryc.cn/news/124344.html

相关文章:

  • Linux学习之防火墙概述
  • JS_围绕圆形滑动
  • Ubuntu上安装RabbitMQ
  • 统计学和机器学习之间的联系和区别
  • linux中profile.d和profile的区别
  • MobaXterm sftp 不能拖拽文件夹了?
  • 【ArcGIS Pro二次开发】(59):Editing(编辑)模块
  • WebSocket与消息推送
  • 5.1 web浏览安全
  • (六)Unity开发Vision Pro——词汇表
  • 算法随笔:图论问题之割点割边
  • 【虚幻引擎】UE5数字人的创建
  • 算法:深度优先遍历
  • Stable Diffusion + Deform制作指南
  • ssm+vue网上花店设计源码和论文
  • 【leetcode】第一章数组
  • 01|Java中常见错误或不清楚
  • 递归的用法和例子
  • 极狐GitLab 企业级 CI/CD 规模化落地实践指南(一)
  • springBoot 简单的demo
  • [国产MCU]-BL602开发实例-实时时钟(RTC)
  • 大数据Flink(六十三):SqlClient工具的使用
  • 哈威比例多路阀控制放大器
  • Java bean 是个什么概念?
  • 微服务系列文章之 Springboot+Vue实现登录注册
  • 【Docker】如何在设计 dockerfile 过程中,设置容器启动后的定时任务
  • 【leetcode】第三章 哈希表part01
  • Docker中Tomcat部署步骤
  • pycharm 安装库
  • 使用 Ploomber、Arima、Python 和 Slurm 进行时间序列预测