You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dsl3/controller/BizExceptionController.java

31 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.aurora.controller;
import com.aurora.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
//业务异常处理控制器用于统一处理BizException类型的异常
@Api(tags = "异常处理模块")
@RestController//返回的结果会直接作为响应体返回
public class BizExceptionController {
@SneakyThrows //用于隐藏异常的显示抛出声明不用写throws Exception等声明
@ApiOperation("/处理BizException")
@RequestMapping("/bizException")//映射请求路径为:/bizException接收该路径的请求来处理异常
public void handleBizException(HttpServletRequest request) {
//HttpServletRequest request通过请求对象获取存储在其中的异常信息
if (request.getAttribute("bizException") instanceof BizException) {
//如果是BizException类型先打印该异常信息再将其强制转换为BizException并抛出
System.out.println(request.getAttribute("bizException"));
throw ((BizException) request.getAttribute("bizException"));
} else {
throw new Exception();//抛出新的Exception
}
}
}