之前用的2.2.x版本的,代码有所变化,废话不多说,直接上干货.
package com.mine.common.auth.exception;
import cn.dev33.satoken.exception.NotLoginException;
import com.mine.common.core.enums.BaseCode;
import com.mine.common.core.web.domain.AjaxResult;
import com.mine.common.core.web.domain.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
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.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
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;
/**
* <p> </p>
*
* @author ZY
* @version 1.0
* @date 2022/3/18 16:34
* @since 0.0.1
*/
@Component
public class FilterErrorController extends BasicErrorController {
public FilterErrorController() {
super(new DefaultErrorAttributes(), new ErrorProperties());
}
/**
* Web页面错误处理
*/
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections
.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);
}
/**
* 除Web页面外的错误处理,比如Json/XML等
*/
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
HttpStatus status = getStatus(request);
if (status == HttpStatus.NO_CONTENT) {
return new ResponseEntity<>(status);
}
Object attribute = request.getAttribute("javax.servlet.error.exception");
String message = "系统异常";
int busCode = 99999;
if (attribute instanceof NotLoginException){
NotLoginException exception = (NotLoginException)attribute;
message = exception.getLocalizedMessage();
busCode = Integer.parseInt(exception.getType());
}
Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
return new ResponseEntity<>(ResponseResult.convertMap(ResponseResult.fail(busCode,message),body), status);
}
}
其实就是抛出我们想抛出的异常,主要在于这句代码:
Object attribute = request.getAttribute("javax.servlet.error.exception");