Amit
Amit

Reputation: 539

Unable to add an instance of HandleInterceptorAdapter

I am trying to intercept every HTTP request that comes into my Spring Boot application. For this I have a LoggingInterceptor as follows:

@Component
public class LoggingInterceptor extends HandlerInterceptorAdapter {

private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);

public LoggingInterceptor() {
    super();
    logger.debug("LoggingInteceptor constructor");
}
@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    logger.debug("LoggingInteceptor: Intercepted  " + request.getRequestURI());
            return true;

}

}

Then, I have an @Configuration class for adding the interceptor to the registry as follows:

@EnableWebMvc
@Configuration
public class LoggingConfig extends WebMvcConfigurerAdapter {
@Autowired
LoggingInterceptor loggingInterceptor;

public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(loggingInterceptor);
}

}

However, I just cannot see any of the logger statements. Is there something more I need to do to get this going?

Upvotes: 0

Views: 192

Answers (1)

luboskrnac
luboskrnac

Reputation: 24561

Your configuration looks fine. Also registry.addInterceptor(loggingInterceptor); call would fail if interceptor would be null, therefore it seems to be correctly wired.

Therefore I would verify:

  1. if DEBUG level us enabled in logging framework
  2. If both classes are included into your Spring context at all (put breakpoint addInterceptors method and start app)

EDIT:

OK, so comment confirmed that you your problem is my first point. You need to find out logging configuration file (e.g. log4j.properties, log4j,xml or logback.xml) and change log level from INFO to DEBUG there.

Upvotes: 1

Related Questions