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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
集成HDFS需要使用Hadoop的API和配置文件。以下是在Spring Boot应用程序中集成HDFS的一般步骤: 1. 添加Hadoop依赖项。在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.3.1</version> </dependency> ``` 2. 添加Hadoop配置文件。在src/main/resources目录下创建一个hadoop文件夹,并在其中添加core-site.xml和hdfs-site.xml文件。这些文件应该包含HDFS的配置信息。 3. 创建HDFS客户端。创建一个HDFS客户端来连接和操作HDFS。以下是一个示例: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HdfsConfiguration { @Value("${hdfs.uri}") private String hdfsUri; @Bean public FileSystem fileSystem() throws IOException { Configuration configuration = new Configuration(); configuration.set("fs.defaultFS", hdfsUri); return FileSystem.get(configuration); } } ``` 4. 使用HDFS客户端进行文件操作。使用上面创建的HDFS客户端进行文件操作,例如上传、下载、删除等操作。以下是一个示例: ```java import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class HdfsService { @Autowired private FileSystem fileSystem; public void uploadFile(String localPath, String remotePath) throws IOException { Path localFilePath = new Path(localPath); Path remoteFilePath = new Path(remotePath); fileSystem.copyFromLocalFile(localFilePath, remoteFilePath); } public void downloadFile(String remotePath, String localPath) throws IOException { Path localFilePath = new Path(localPath); Path remoteFilePath = new Path(remotePath); fileSystem.copyToLocalFile(remoteFilePath, localFilePath); } public void deleteFile(String remotePath) throws IOException { Path remoteFilePath = new Path(remotePath); fileSystem.delete(remoteFilePath, true); } } ``` 以上是一些基本步骤,您可以根据自己的需要进行更改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值