当前位置:   article > 正文

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

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
  • 1
  • 2
  • 3

原代码

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

新代码:

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

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/850682
推荐阅读
相关标签
  

闽ICP备14008679号