[JAVA] Get the path defined in Controller class of Spring boot as a list

When creating sitemap.xml of the site built with SpringBoot, I checked how to get the value of @RequestMapping annotation attached to the method of Controller class, so make a note.


Ignore exceptions and path variables, and get the path string defined in the annotation as a list anyway.


import java.util.Arrays;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException {
        ClassPathScanningCandidateComponentProvider scanner =
                new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
        Set<BeanDefinition> beanSet = scanner.findCandidateComponents("/* controller package string */");
        for (BeanDefinition def : beanSet) {
            Class<?> clazz = Class.forName(def.getBeanClassName());
            Arrays.stream(clazz.getDeclaredMethods()).map(m -> m.getAnnotation(RequestMapping.class)).filter(
                    a -> a != null && a.value().length > 0).forEach(a -> Arrays.stream(a.value()).forEach(p ->{

The point

The part that gets the metadata of spring using ClassPathScanningCandidateComponentProvider It's the liver. Other than that, I usually get the method by reflection and steadily look at the value of the annotation. To make it a realistic sitemap, you need to be careful about replacing variables with expected values, excluding private urls, and so on.


How to get the class in which annotations are defined when Java is executed

Recommended Posts

Get the path defined in Controller class of Spring boot as a list
Get a proxy instance of the component itself in Spring Boot
[rails] List of actions defined in Controller
Get a list of other sessions of the same user when using Redis Session in Spring Boot (2 series). Also discard it.
How to get the absolute path of a directory running in Java
Specify the encoding of static resources in Spring Boot
Get the name of the test case in the JUnit test class
Get a list of classes in a Guava specific package
[Java] Get the file path in the folder with List
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
A story packed with the basics of Spring Boot (solved)
A program that counts the number of words in a List
[Spring Boot] Role of each class
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
How to output a list of strings in JSF as comma-separated strings
[Spring Boot] Until @Autowired is run in the test class [JUnit5]
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
Get the result of POST in Java
Autowired fields in a class that inherits TextWebSocketHandler in Spring Boot become NULL
Procedure to make the value of the property file visible in Spring Boot
How to get the current date as a string in yyyyMMdd format
[Java] How to get to the front of a specific string using the String class
[Spring Boot] List of validation rules that can be used in the property file for error messages
graphql-ruby: How to get the name of query or mutation in controller Note
Introducing the Spring Boot Actuator, a function that makes the operation of Spring Boot applications easier.
How to get the ID of a user authenticated with Firebase in Swift
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
Call a method of the parent class by explicitly specifying the name in Ruby
Sign in to a Spring Boot web application on the Microsoft ID platform
Set the source of the library set as a dependency in IntelliJ as a separate module of the project
How to set environment variables in the properties file of Spring boot application
I want to recursively get the superclass and interface of a certain class
Test controller with Mock MVC in Spring Boot
Run a Spring Boot project in VS Code
Measure the size of a folder in Java
When you get lost in the class name
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
How to add a classpath in Spring Boot
Java tips-Create a Spring Boot project in Gradle
A quick review of Java learned in class
View the Gradle task in the Spring Boot project
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Takes out the HashMap value and returns the result sorted by the value of value as a list.
[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
Customize the display when an error such as 404 Not Found occurs in Spring Boot
Get multiple Resources that match the pattern in spring
Count the number of occurrences of a string in Ruby
I checked asynchronous execution of queries in Spring Boot 1.5.9
Get a list of MBean information for Java applications
A quick review of Java learned in class part4
Return the execution result of Service class in ServiceResponse class
[For beginners] DI ~ The basics of DI and DI in Spring ~
A memorandum of addiction to Spring Boot2 x Doma2
Access the built-in h2db of spring boot with jdbcTemplate
How to create a Spring Boot project in IntelliJ
I tried to reduce the capacity of Spring Boot
[Ruby / Rails] Set a unique (unique) value in the class