Skip to content

Parameter names of the handler method are null in HandlerInterceptor::preHandle during first invocation of an endpoint #36024

@Walti91

Description

@Walti91

The parameter names from the handler method that is passed to the preHandle method of a HandlerInterceptor are null when the endpoint is called for the first time after the application has been started.

Steps to reproduce:

  1. Create a project with the Spring Initializr using Spring Boot version 4.0.0 and the Spring Web Dependency.
  2. Add the following code in the DemoApplication
package com.example.demo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@RestController
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

    @GetMapping("test")
    public void test(@RequestParam String test) {

    }

    @Bean
    WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new TestHandlerInterceptor());
            }
        };
    }

    static class TestHandlerInterceptor implements HandlerInterceptor {

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler) {
            if (handler instanceof HandlerMethod handlerMethod) {
                System.out.println(handlerMethod);

                for (var methodParameter : handlerMethod.getMethodParameters()) {
                    System.out.println("Parameter Index: " + methodParameter.getParameterIndex());
                    System.out.println("Parameter Type: " + methodParameter.getParameterType());
                    System.out.println("Parameter Name: " + methodParameter.getParameterName());
                }
            }

            return true;
        }
    }
}
  1. Start the application
  2. Call the endpoint: curl localhost:8080/test?test=test
  3. The console output shows null for the parameter name
com.example.demo.DemoApplication#test(String)
Parameter Index: 0
Parameter Type: class java.lang.String
Parameter Name: null
  1. Call the endpoint again the same way.
  2. Now the console output has the parameter name set as expected.
com.example.demo.DemoApplication#test(String)
Parameter Index: 0
Parameter Type: class java.lang.String
Parameter Name: test

As far as i can tell the parameter names are only null during the first call per endpoint in the application and the problem also seems to be isolated to the preHandle method in the Interceptor. The values are correct in the postHandle even during the first call for a endpoint.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions