[JAVA] Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot

1. What you want to do

--Get the class name and method name of Controller executed by HandlerInterceptor

2. Version, conditions, etc.

3. What I did

DemoInterceptor.java


public class DemoInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        // (1)Skip if not HandlerMethod
        //js or org.springframework.web.servlet.resource.Become a ResourceHttpRequestHandler
        if (!HandlerMethod.class.isInstance(handler)) {
            return true;
        }

        // (2)Cast handler to HandlerMethod
        HandlerMethod handlerMethod = HandlerMethod.class.cast(handler);

        // (3)Class name of Controller to be executed
        String beanTypeName = handlerMethod.getBeanType().getName();

        // (4)Method name of the method to be executed
        String methodName = handlerMethod.getMethod().getName();

        // (5)Short log message
        //FQCN as follows#MethodName[Arguments]Can be obtained
        // [example]
        // case1) No arguments
        // jp.gr.java_conf.pekokun.web.app.HelloController#say[0 args]
        // case2) 1 argument
        // jp.gr.java_conf.pekokun.web.app.HelloController#say[1 args]
        String shortLogMessage = handlerMethod.getShortLogMessage();

        // (6)You can also get method parameter information
        // handlerMethod.getMethodParameters()...

        return super.preHandle(request, response, handler);
    }

end.

Recommended Posts

Get the class name and method name of Controller executed by HandlerInterceptor of Spring Boot
Get the path defined in Controller class of Spring boot as a list
Call a method of the parent class by explicitly specifying the name in Ruby
[Kotlin] Get the argument name of the constructor by reflection
Get the name of the test case in the JUnit test class
Spring boot controller method memo
How to get the class name / method name running in Java
Get the object name of the instance created by the new operator
Branch processing by the return value of RestTemplate and the status code of ResponseEntity in Spring Boot
[Spring Boot] Role of each class
Get a proxy instance of the component itself in Spring Boot
Method to add the number of years and get the end of the month
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
graphql-ruby: How to get the name of query or mutation in controller Note
[Rails] Get the path name of the URL before the transition and change the link destination
I want to recursively get the superclass and interface of a certain class
When you get lost in the class name
Customize the bean name set by Spring component-scan
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
About next () and nextLine () of the Scanner class
◆ Get API created by Spring Boot from React
The official name of Spring MVC is Spring Web MVC
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
Provisional memo when the name of the method parameter of the Java class cannot be obtained by reflection in the Eclipse plug-in project.
How to implement authentication process by specifying user name and password in Spring Boot
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Spring Boot] The story that the bean of the class with ConfigurationProperties annotation was not found
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
Specify the encoding of static resources in Spring Boot
Get the anime name for this term by scraping
[For beginners] DI ~ The basics of DI and DI in Spring ~
Access the built-in h2db of spring boot with jdbcTemplate
[Ruby] Method to easily get the receiver type .class
I tried to reduce the capacity of Spring Boot
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Ruby, Nokogiri: Get the element name of the selected node
How to boot by environment with Spring Boot of Maven
[Ruby] Class nesting, inheritance, and the basics of self
Automatic assignment of name attribute and id attribute by form_with
Why the get method is needed in the Calendar class
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
About the function of Spring Boot due to different versions
[Ruby] Questions and verification about the number of method arguments
When using the constructor of Java's Date class, the date advances by 1900.
A story packed with the basics of Spring Boot (solved)
Deploy the application created by Spring Boot to Heroku (public) ②
Get only the ID of the container specified by docker ps
Change the file name and output destination of the JavaVM error file
Deploy the application created by Spring Boot to Heroku (public) ①
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
Summary of values returned by the Spliterator characteristics method #java
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
[Rails/CarrierWave/MiniMagick] Give the CSS class name for portrait and landscape to the img tag of the uploaded image.
[Rails] Get a unique record from the table joined by the join method of Active Record (Rails Tutorial Chapter 14)