【问题标题】:Using spring WebFlux, how can I get all the RequestHandlerMappings?使用spring WebFlux,我怎样才能得到所有的RequestHandlerMappings?
【发布时间】:2021-12-17 10:04:44
【问题描述】:

在 servlet 世界中,我可以执行以下操作:

public class MyListener implements ApplicationListener<ApplicationReadyEvent> {

  @Override
  public void onApplicationEvent(ApplicationReadyEvent event) {
    ApplicationContext context = event.getApplicationContext();
    Map<RequestMappingInfo, HandlerMethod> endpoints = context.getBean(RequestMappingHandlerMapping.class).getHandlerMethods();

    endpoints.forEach((info, method) -> {
      log.info("Info: {}/n method: {}", info, method);
    });
  }
}

但是,由于 RequestMappingHandlerMapping Bean 未注册,因此在 WebFlux 服务器中同样不起作用。

这里的正确方法是什么?这甚至可能吗?

我可以将活动保持更长时间,并彻底尝试找到上面有 @RestController 的类。但在这种情况下,我不知道潜在的 Controller 类的包名称。

【问题讨论】:

  • 使用来自org.springframework.web.reactive.result.method.annotationRequestMappingHandlerMapping
  • 天哪,我很震惊,这很容易。如果你愿意,请提交一个答案,我会将其标记为正确@SotiriosDelimanolis 我没有注意到包名完全不同.. 在 spring javadoc 和源代码中从同一个类名开始花费了这么多时间......
  • 是的,他们为 webflux 包重用了很多类名。不过没关系,我懒得回答了。如果你愿意,你可以自己回答。
  • 谢谢,我已经完成了,等待期过后会确认

标签: java spring spring-boot reflection spring-webflux


【解决方案1】:

Spring boot 重用了很多类。具体来说,有两个RequestMappingHandlerMapping

对于 servlet 使用,您要导入:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

但对于反应式使用,它是:

org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping

在创建同时支持 Reactive 和 Servlet 使用的库时要小心 - 很容易导入不正确的类

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多