Spring boot 2.0 升级到 3.3.1 的相关问题 (二)

2024-07-19 1502阅读

文章目录

  • Spring boot 2.0 升级到 3.3.1 的相关问题 (二)
    • 自定义错误处理页面的问题
      • 问题描述
      • 问题解决
      • spring.factories 废弃的问题
        • 问题描述
        • 问题解决

          Spring boot 2.0 升级到 3.3.1 的相关问题 (二)

          自定义错误处理页面的问题

          问题描述

          AbstractErrorController 移除了getErrorPath的方法,并准对getErrorAttributes方法增加了ErrorAttributeOptions参数,用于获取属性中的额外参数信息。因此需要对代码原代码进行相应的改造

          Spring boot 2.0 升级到 3.3.1 的相关问题 (二)
          (图片来源网络,侵删)

          问题解决

          参考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> error(HttpServletRequest request,
                      HttpServletResponse response){
                  HttpStatus status = getStatus(request);
                  Map 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.factories 废弃的问题

          问题描述

          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
          
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]