SpringBoot——启动服务时获取所有controller层的接口url列表

需求

  在服务启动时,获取服务的所有controller层接口url列表,通过CommandLineRunner实现服务启动时的操作。

  1. CommandLineRunnerSpringApplication.run运行完成之后后才会运行自己创建的实现类。
  2. 加入@Component注解后,就可以将对象交给spring管理。
  3. 加入@Order()注解控制顺序,数字越小越靠前。

技术应用

  • CommandLineRunner
  • WebApplicationContext
  • RequestMappingHandlerMapping

CommandLineRunner源码

package org.springframework.boot;

@FunctionalInterface
public interface CommandLineRunner {
    void run(String... args) throws Exception;
}
  1. CommandLineRunner是一个接口,我们可以自定义实现该接口,并具体实现run方法。
  2. 如果在上下文中,若有多个实现该接口的类,就需要通过@Order注解进行加载顺序的指定。

代码模板

@Slf4j
@Component
@Order(1)
@RequiredArgsConstructor
public class CommandLineInitResource implements CommandLineRunner {

    /**
     * 上下文
     */
    @Autowired
    WebApplicationContext applicationContext;

	   @Value("${route.prefix}")
	   private String routePrefix;

    @Override
    public void run(String... args) throws Exception {

        //获取controller相关bean
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        //获取method
        Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
        //构造url list
        List<String> urlList = new ArrayList<>();
        //获取methodMap的key集合
        for (RequestMappingInfo info : methodMap.keySet()) {
            //controller url集合
            Set<String> urlSet = info.getPatternsCondition().getPatterns();
            //controller url拼接路由前缀
            urlList.addAll(urlSet.stream().map(url ->
                    //校验前缀
                    routePrefix.startsWith("/") ? (routePrefix + url) : ("/" + routePrefix + url)
            ).collect(Collectors.toList()));
			//获取所有方法类型
			//Set<RequestMethod> methodSet = info.getMethodsCondition().getMethods();
        }
        log.info("web controller urlList: {}", urlList);
    
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个快速开发的Java web框架,可以大大简化开发过程中的配置和部署。在Spring Boot中,我们可以使用注解来标注一个controller,使其对应于一个特定的URL。那么,如何获取URL对应的controller方法呢? 在Spring Boot中,我们可以使用@GetMapping注解来标注一个controller的方法,其参数为请求的URL,如下所示: ``` @RestController @RequestMapping("/user") public class UserController { @GetMapping("/info") public String getUserInfo() { return "user info"; } } ``` 在上面的例子中,使用了@RestController和@RequestMapping来标注controller,其中@RequestMapping指定了整个controller对应的URL,@GetMapping则指定了getUserInfo()方法对应的URL,即/user/info。如果我们要获取URL对应的controller方法,我们可以使用RequestMappingHandlerMapping类的getHandler()方法,如下所示: ``` @Autowired private RequestMappingHandlerMapping handlerMapping; public Method getHandlerMethod(String url) { HandlerExecutionChain chain = handlerMapping.getHandler(url); HandlerMethod handlerMethod = (HandlerMethod) chain.getHandler(); return handlerMethod.getMethod(); } ``` 在上面的例子中,我们注入了RequestMappingHandlerMapping类,通过调用getHandler()方法获取HandlerExecutionChain对象,通过该对象的getHandler()方法获取到对应的handlerMethod,最后使用handlerMethod的getMethod()方法获取对应的方法即可。使用该方法,我们就可以根据URL获取Spring Bootcontroller方法信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值