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

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

拦截器Interceptor的变动

问题介绍

在2.0 版本可以通过继承org.springframework.web.servlet.handler.HandlerInterceptorAdapter 类来实现一个拦截器,在2.4.0 版本开始标记为弃用,在3.3.1 版本已经没有这个类了,需要使用新的方式来实现。

解决方案

直接实现 org.springframework.web.servlet.HandlerInterceptor 接口即可。

原代码:

import com.abc.springboot.frame.constant.FrameConstant;
import com.abc.springboot.frame.pojo.dto.SystemSecurityRequestDTO;
import com.abc.springboot.frame.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 检查客户端版本号拦截器
 */
@Slf4j
public class CheckClientVersionInterceptor extends HandlerInterceptorAdapter {

    /**
     * 检查客户端版本是否有效
     */
    @Autowired
    private ICheckClientVersionHandler checkClientVersionHandler;

    /**
     * 请求处理前处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        //获取请求参数
        SystemSecurityRequestDTO requestDTO = RequestUtils.getAndSetSystemSecurityRequestDTO(request);
        //校验客户端版本号
        try{
            boolean checkResult =  checkClientVersionHandler.checkClientVersion(requestDTO,
                request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_VERSION),
                request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_TYPE));
            if(!checkResult){
                log.info("版本号不支持【{}】【{}】",requestDTO.getMethod(),requestDTO.getUri());
                request.getRequestDispatcher(FrameConstant.APPLICATION_URL_CLIENT_VERSION_VERIFY_FAILED).forward(request, response);
                return false;
            }
            return true;
        }catch (Exception e){
            log.warn("记录系统请求日志失败。",e);
            return false;
        }
    }
}

新代码


import com.abc.springboot.frame.constant.FrameConstant;
import com.abc.springboot.frame.pojo.dto.SystemSecurityRequestDTO;
import com.abc.springboot.frame.utils.RequestUtils;
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.web.servlet.HandlerInterceptor;


/**
 * 检查客户端版本号拦截器
 */
@Slf4j
public class CheckClientVersionInterceptor implements HandlerInterceptor {

    /**
     * 检查客户端版本是否有效
     */
    @Autowired
    private ICheckClientVersionHandler checkClientVersionHandler;

    /**
     * 请求处理前处理
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        //获取请求参数
        SystemSecurityRequestDTO requestDTO = RequestUtils.getAndSetSystemSecurityRequestDTO(request);
        //校验客户端版本号
        try{
            boolean checkResult =  checkClientVersionHandler.checkClientVersion(requestDTO,
                request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_VERSION),
                request.getHeader(FrameConstant.HTTP_HEADER_CLIENT_TYPE));
            if(!checkResult){
                log.info("版本号不支持【{}】【{}】",requestDTO.getMethod(),requestDTO.getUri());
                request.getRequestDispatcher(FrameConstant.APPLICATION_URL_CLIENT_VERSION_VERIFY_FAILED).forward(request, response);
                return false;
            }
            return true;
        }catch (Exception e){
            log.warn("记录系统请求日志失败。",e);
            return false;
        }
    }
}

WebMvcConfigurerAdapter 自定义Mvc配置

问题介绍

在2.0 版本可以通过继承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter 类来实现自定义Mvc拦截器,在2.4.0 版本开始标记为弃用,在3.3.1 版本已经没有这个类了,需要使用新的方式来实现。

解决方案

org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter 类在 Spring Framework 5.0 之后被标记为已弃用,并在 Spring Boot 2.0 中不再推荐使用 。

替代方案有两种:

直接实现 WebMvcConfigurer 接口:
这是官方推荐的替代方法。WebMvcConfigurer 接口提供了多种默认方法(即带有实现的方法),允许开发者只实现所需的配置方法,而不必要实现接口中的所有方法。这种方式不会影响 Spring Boot 自身的 @EnableAutoConfiguration,允许 Spring Boot 的自动配置生效 。

继承 WebMvcConfigurationSupport 类:
另一种方法是继承 WebMvcConfigurationSupport 类。这个类提供了 Spring MVC 的默认配置,通过继承它,可以覆盖特定的方法来自定义配置。但请注意,使用这种方式将覆盖 Spring Boot 的自动配置,因此如果某个方法没有被重写,可能会导致相关功能的缺失,比如静态资源的处理 。

总结来说,如果你需要进行一些简单的自定义配置,并且想要保留 Spring Boot 的自动配置功能,推荐直接实现 WebMvcConfigurer 接口。如果你需要更全面的控制 Spring MVC 的配置,可以考虑继承 WebMvcConfigurationSupport 类,但要确保所有必要的配置都被正确覆盖和实现。

原代码

import com.abc.utils.formatter.LocalDateTimeFormatter;
import com.abc.utils.formatter.StringFormatter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.time.LocalDateTime;

/**
 * 自定义的Mvc配置,用于配置格式化程序
 * @author 徐明龙 XuMingLong 2022-03-17
 */
@Configuration
public class CustomWebMvcFormattersConfigurer extends WebMvcConfigurerAdapter  {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        //仅对Path方式传入的参数生效
        registry.addFormatterForFieldType(String.class, new StringFormatter());
        registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter());
    }
}

新代码

import com.abc.utils.formatter.LocalDateTimeFormatter;
import com.abc.utils.formatter.StringFormatter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDateTime;

/**
 * 自定义的Mvc配置,用于配置格式化程序
 * @author 徐明龙 XuMingLong 2022-03-17
 */
@Configuration
public class CustomWebMvcFormattersConfigurer implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        //仅对Path方式传入的参数生效
        registry.addFormatterForFieldType(String.class, new StringFormatter());
        registry.addFormatterForFieldType(LocalDateTime.class, new LocalDateTimeFormatter());
    }
}

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
集成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); } } ``` 以上是一些基本步骤,您可以根据自己的需要进行更改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值