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

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

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

问题描述

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.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

相关推荐

  1. Spring boot 2.0 升级 3.3.1 相关问题

    2024-07-15 20:20:03       21 阅读
  2. springboot1.x升级springboot3.x中遇到问题总结

    2024-07-15 20:20:03       51 阅读
  3. Spring boot 2.0 升级 3.3.1 相关问题 (一)

    2024-07-15 20:20:03       17 阅读
  4. Spring boot 2.0 升级 3.3.1 相关问题 (三)

    2024-07-15 20:20:03       22 阅读
  5. Spring boot 2.0 升级 3.3.1 相关问题 (四)

    2024-07-15 20:20:03       16 阅读
  6. 升级springboot3.2集成shiro问题

    2024-07-15 20:20:03       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 20:20:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 20:20:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 20:20:03       57 阅读
  4. Python语言-面向对象

    2024-07-15 20:20:03       68 阅读

热门阅读

  1. LeetCode题练习与总结:寻找峰值--162

    2024-07-15 20:20:03       17 阅读
  2. Mysql数据库(一)

    2024-07-15 20:20:03       24 阅读
  3. (leetcode学习)16. 最接近的三数之和

    2024-07-15 20:20:03       19 阅读
  4. /EtherCATInfo/Descriptions/Devices/Device/SubDevice/@Hideable

    2024-07-15 20:20:03       16 阅读
  5. 零基础自学爬虫技术该从哪里开始入手?

    2024-07-15 20:20:03       19 阅读
  6. FeignClient详解

    2024-07-15 20:20:03       21 阅读
  7. 【经验】LiveData使用常见问题

    2024-07-15 20:20:03       21 阅读
  8. A2A VPN简介

    2024-07-15 20:20:03       20 阅读
  9. c++多态详细学习

    2024-07-15 20:20:03       16 阅读
  10. 注册登录后上传文件到本地数据库项目

    2024-07-15 20:20:03       16 阅读