[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
SpringBoot提供了一套默认的异常处理机制,一旦程序异常,springboot会向/error的url发起请求,并且提供了名为BasicErrorController的拦截器来处理请求,因此我们可以自定义显示的错误页面。
基于现在越来越多的前后端分离项目,我们返回的错误信息往往不再是错误页面,而是携带错误信息的字符串,因此我们需要自定义异常拦截器去拦截错误信息并将信息已正确的格式返回给前端。
已定义异常处理拦截器主要有几种方式:
* 通过@ExceptionHandler注解处理异常,该方式中处理异常的方法必须和产生异常的方法位于同一个controller之中,容易产生代码冗余。
*
通过@ControllerAdvice和ExceptionHandler注解实现全局异常处理类去处理异常,该方式中每处理一个异常需要添加一个方法,同样会产生代码冗余。
*
通过SimpleMappingExceptionResolver对象处理异常,该方式必须必须返回SimpleMappingExceptionResolver对象,不会产生代码冗余,但无法获取到具体的错误信息,只能知道是发生了什么错误。
*
通过自定义HandlerExceptionResolver对象处理异常,该方式必须要实现HandlerExceptionResolver接口,不会产生代码冗余,也可以获取到具体错误信息。
我使用的时第二种方式,捕获自定格式的异常信息并进行返回:
package com.weixin.springbootweixin.config; import
com.weixin.springbootweixin.entity.ExceptionApi; import
org.springframework.web.bind.annotation.ControllerAdvice; import
org.springframework.web.bind.annotation.ExceptionHandler; import
org.springframework.web.servlet.ModelAndView; import
org.springframework.web.servlet.view.json.MappingJackson2JsonView;
@ControllerAdvice public class GlobalException { @ExceptionHandler(value =
{com.weixin.springbootweixin.entity.ExceptionApi.class})
//遇到异常信息格式是自定义的异常格式时进行处理 public Object ApiException(ExceptionApi e){ int code =
e.getCode(); String message = e.getMessage(); Object data = e.getData();
//将异常信息封装成json格式返回给前端 ModelAndView modelAndView = new ModelAndView(new
MappingJackson2JsonView()); modelAndView.addObject("code", code);
modelAndView.addObject("message", message); modelAndView.addObject("data",
data); return modelAndView; } }