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

news/2024/8/22 10:23:36 标签: spring boot, 后端, java

文章目录

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

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

原代码

java">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());
        }
       
    }

}

新代码:

java">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

http://www.niftyadmin.cn/n/5556380.html

相关文章

【python】Pandas中`ValueError: cannot reindex from a duplicate axis`错误分析

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

ArcGIS Enterprise 命令行组件创建配置

1. 创建ArcGIS Server站点 使用 createsite工具 命令行直接执行 createsite.sh [-u <arg>] [-p <arg>] [-d <arg>] [-c <arg>]执行文件 createsite.sh [-f <FILE>]安装目录下会有类似的创建站点文件&#xff1a; 修改其中的内容&#xff0c;…

golang AST语法树解析

1. 源码示例 package mainimport ("context" )// Foo 结构体 type Foo struct {i int }// Bar 接口 type Bar interface {Do(ctx context.Context) error }// main方法 func main() {a : 1 }2. Golang中的AST golang官方提供的几个包&#xff0c;可以帮助我们进行A…

构建加速秘籍:精通Gradle构建启动参数

构建加速秘籍&#xff1a;精通Gradle构建启动参数 Gradle作为一种灵活且功能丰富的构建工具&#xff0c;提供了多种启动参数来自定义和优化构建过程。这些参数不仅可以提高构建速度&#xff0c;还能增强构建的可预测性和可控性。本文将详细解释如何在Gradle中使用构建启动参数…

JS【实战】CSS 样式相关的处理

CSS 样式键转换 如 fontSize -> font-size // 函数封装 -- CSS样式键转换&#xff0c;如 fontSize -> font-size function CSSformatKey(oldKey) {// 查找所有大写字母&#xff0c;转换为 "-小写"const newKey oldKey.replace(/[A-Z]/g, (c) > -${c.toLoc…

数据结构小测试:排序算法

目录 1、请简述数据结构八大排序算法的思路。 2、常用排序算法手写 冒泡排序&#xff1a; 选择排序&#xff1a; 快速排序&#xff1a; 归并排序&#xff1a; 堆排序&#xff1a; 3、额外再加一个二分查找吧 1、请简述数据结构八大排序算法的思路。 冒泡排序&#xff…

打破平台限制,使智能手机和平板电脑上无缝运行Windows x86/x64架构的软件和游戏的一款安卓应用

大家好&#xff0c;今天给大家分享一款专为Android设备设计的模拟器应用Winlator。其核心功能是能够在基于ARM架构的智能手机和平板电脑上无缝运行Windows x86/x64架构的软件和游戏。 Winlator是一款Android应用程序&#xff0c;它允许用户使用Wine和Box86/Box64在Android设备上…

LT_0002_回文数

一、题目描述 二、代码实现 2.1 将数字转为字符串然后反转 package com.it.leetcode;public class Day_0002 {public static void main(String[] args) {System.out.println(isPalindrome(121)); //trueSystem.out.println(isPalindrome(123)); //falseSystem.out.println(is…