gpt4 book ai didi

java - 将 HandlerInterceptor 迁移到 Spring boot 2.6

转载 作者:行者123 更新时间:2023-12-05 01:51:35 25 4
gpt4 key购买 nike

我为 Spring 2.4 实现了这个旧代码

public class Interceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
........
return super.preHandle(request, response, handler);
}
}

我将代码迁移到 Spring 2.6:

public class Interceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
......................
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}

我得到了 Cannot resolve method 'preHandle' in 'Object' 所以我将代码更改为 HandlerInterceptor.super.preHandle(request, response, handler);

这样编辑代码是否正确:HandlerInterceptor.super.preHandle(request, response, handler); 还是应该以其他方式编辑?

最佳答案

应该是

public class Interceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
......................
return true;
}
}

请注意,该方法返回一个 boolean 值。它告诉 Spring 进一步处理请求 (true) 或不 (false)。

HandlerInterceptorpreHandle() 的默认实现仅返回 true ( https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html#preHandle-javax.servlet.http.HttpServletRequest-javax.servlet.http.HttpServletResponse-java.lang.Object- )

关于java - 将 HandlerInterceptor 迁移到 Spring boot 2.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72152105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com