赞
踩
AbstractErrorController 移除了getErrorPath
的方法,并准对getErrorAttributes
方法增加了ErrorAttributeOptions
参数,用于获取属性中的额外参数信息。因此需要对代码原代码进行相应的改造
参考org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController
类来改造自定义的错误处理页面。
当然自定义页面后就无法使用下面这些配置了,如果当然也可以参考BasicErrorController
自己实现一遍。
server.error.include-message=always
server.error.include-binding-errors=always
server.error.include-exception=true
原代码
import com.abc.commons.source.pojo.ResponseResult; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import springfox.documentation.annotations.ApiIgnore; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * 自定义错误处理 */ @Controller @Slf4j @ApiIgnore public class GlobalErrorController extends AbstractErrorController { private static final String ERROR_PATH = "/error"; @Autowired private ErrorAttributes errorAttributes; public GlobalErrorController(ErrorAttributes errorAttributes) { super(errorAttributes); } @Override public String getErrorPath() { return ERROR_PATH; } @RequestMapping(value = ERROR_PATH) public ResponseEntity<ResponseResult<?>> error(HttpServletRequest request, HttpServletResponse response){ HttpStatus status = getStatus(request); Map<String,Object> errorAttributes = getErrorAttributes(request, true); log.info("异常信息【{}】",errorAttributes); switch (status) { //404 case NOT_FOUND: log.info("【{}】资源不存在", errorAttributes.get("path")); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ResponseResult.notFound()); default: log.error("系统出错【{}】",status); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResponseResult.systemError()); } } }
新代码:
import com.abc.commons.source.pojo.ResponseResult; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController; import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import springfox.documentation.annotations.ApiIgnore; import java.util.Map; /** * 自定义错误处理 */ @Controller @Slf4j @ApiIgnore @RequestMapping("${server.error.path:${error.path:/error}}") public class GlobalErrorController extends AbstractErrorController { @Autowired private ErrorAttributes errorAttributes; public GlobalErrorController(ErrorAttributes errorAttributes) { super(errorAttributes); } @RequestMapping public ResponseEntity<ResponseResult<?>> error(HttpServletRequest request, HttpServletResponse response){ HttpStatus status = getStatus(request); Map<String,Object> errorAttributes = getErrorAttributes(request, ErrorAttributeOptions.defaults()); log.info("异常信息【{}】",errorAttributes); return switch (status) { case NOT_FOUND ->{ log.info("【{}】资源不存在", errorAttributes.get("path")); yield ResponseEntity.status(HttpStatus.NOT_FOUND).body(ResponseResult.notFound()); } default -> { log.error("系统出错【{}】",status); yield ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResponseResult.systemError()); } }; } }
Spring Boot 3.0
中自动配置注册的 META-INF/spring.factories
写法已废弃,改为了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
写法,这导致 starter 自动配置没有改造的都会失效。
在新增``META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`,
原 spring.factories
配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.abc.spring.boot.ALiYunOSSUtilsAutoConfigure,\
com.abc.spring.boot.EmailClientAutoConfigure,\
com.abc.spring.boot.EsClientAutoConfigure,\
com.abc.spring.boot.FtpUtilClientAutoConfigure,\
com.abc.spring.boot.DingDingApiAutoConfigure,\
com.abc.spring.boot.ALiYunRocketMqProducerAutoConfigure,\
com.abc.spring.boot.ALiYunSTSUtilsAutoConfigure,\
com.abc.spring.boot.ALiCloudApiStoreConfigure,\
com.abc.spring.boot.TycApiAutoConfigure
新org.springframework.boot.autoconfigure.AutoConfiguration.imports
配置
com.abc.spring.boot.ALiYunOSSUtilsAutoConfigure
com.abc.spring.boot.EmailClientAutoConfigure
com.abc.spring.boot.EsClientAutoConfigure
com.abc.spring.boot.FtpUtilClientAutoConfigure
com.abc.spring.boot.DingDingApiAutoConfigure
com.abc.spring.boot.ALiYunRocketMqProducerAutoConfigure
com.abc.spring.boot.ALiYunSTSUtilsAutoConfigure
com.abc.spring.boot.ALiCloudApiStoreConfigure
com.abc.spring.boot.TycApiAutoConfigure
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。