Spring Boot 获取所有 SpringMVC Controller 路径

Spring Boot SpringMVC RBAC About 3,445 words

使用场景

在使用RBAC权限管理时,往往需要添加后端API接口路径,可以使用此方法获取Spring Boot中定义的所有Controller路径。

RequestMapping中配置的value字段为url路径,name字段可以为接口的注释。

获取自定义路径

@SpringBootApplication
public class GetAllControllerPathApplication implements ApplicationRunner {

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

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<RequestMappingInfo, HandlerMethod> methodMap = requestMappingHandlerMapping.getHandlerMethods();
        Map<String, String> urlMap = new HashMap<>();
        for (RequestMappingInfo info : methodMap.keySet()){
            //获取请求路径
            Set<String> directPaths = info.getDirectPaths();
            // 获取全部请求方式
            //Set<RequestMethod> Methods = info.getMethodsCondition().getMethods();
            //获取全部请求名称
            String urlName =  info.getName();
            for (String url : directPaths){
                System.out.println("urlname#" + urlName + ", url#" + url);
            }
        }
    }
}

获取所有路径

@SpringBootApplication
public class GetAllControllerPathApplication implements ApplicationRunner {

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

    @Autowired
    WebApplicationContext applicationContext;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<String, RequestMappingInfoHandlerMapping> map = applicationContext.getBeansOfType(
            RequestMappingInfoHandlerMapping.class);

        map.forEach((s, requestMappingInfoHandlerMapping) -> {
            System.out.println("-------------- " + s + " --------------");
            Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingInfoHandlerMapping.getHandlerMethods();
            handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
                System.out.println(requestMappingInfo.getDirectPaths() + ", name#" + requestMappingInfo.getName() + ", method#" + requestMappingInfo.getMethodsCondition().getMethods());
            });
        });

    }
}

不能直接注入RequestMappingInfoHandlerMapping,否则会报错:

Field requestMappingInfoHandlerMapping in com.example.getallcontrollerpath.GetAllControllerPathApplication required a single bean, but 4 were found:
    - requestMappingHandlerMapping: defined by method 'requestMappingHandlerMapping' in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
    - healthEndpointWebMvcHandlerMapping: defined by method 'healthEndpointWebMvcHandlerMapping' in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointWebExtensionConfiguration$MvcAdditionalHealthEndpointPathsConfiguration.class]
    - webEndpointServletHandlerMapping: defined by method 'webEndpointServletHandlerMapping' in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.class]
    - controllerEndpointHandlerMapping: defined by method 'controllerEndpointHandlerMapping' in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.class]
Views: 3,028 · Posted: 2022-10-09

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh