Invalid springboot2 interceptor

found a lot on the Internet, basically the same, step by step, are invalid, the following is my configuration:

Config:

import com.example.demo.interceptor.OneInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ Configuration
public class InterceptorConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**");
}

}

OneInterceptor:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

@ Component
public class OneInterceptor implements HandlerInterceptor {

private Logger logger = LoggerFactory.getLogger(OneInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
    logger.debug("");
    return false;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    System.err.println("post");
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    System.err.println("after");
}

}

No matter what path you visit, you won"t use the interceptor.
I don"t know where the configuration is wrong. Ask for a solution!

Apr.28,2021

I don't know if "intercept" will be printed when accessing the path. " ?

the preHandle method of OneInterceptor above will not be executed until it returns true. If you add @ Component, you don't need to new.

@Component
public class OneInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
        System.out.println("pre");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.err.println("post");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.err.println("after");
    }
}
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    private final OneInterceptor oneInterceptor;

    @Autowired
    public InterceptorConfig(OneInterceptor oneInterceptor) {
        this.oneInterceptor = oneInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(oneInterceptor).addPathPatterns("/**");
    }
}

you have no problem with this configuration. Check the log to see if there is logger.debug ("intercept.") ;
in addition, after OneInterceptor is declared as a component, it can be injected when new, is not needed

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3aaf7-2c24a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3aaf7-2c24a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?