繁体   English   中英

在Spring Web中,如何获取@RestController的完整URL?

[英]In Spring Web, how do I get the full URL of a @RestController?

可以说我将ApplicationContext注入到bean中。 然后,我可以发现所有用RestController注释的bean。 我将如何发现与所述bean相关联的基本URL?

例:

@Component
public class ServiceEnumerator {
  @Autowired
  private ApplicationContext context;

  @PostConstruct
  private void postConstruct() {
    final Map<String, Object> beansToExpose = context.getBeansWithAnnotation(RestController.class);
  }
}

按照标准,基本URL(上下文根)需要在配置文件中设置。 不确定以下内容是否对您有帮助。...尝试在控制器中注入HttpServletRequest,然后从控制器中将其传递到所需的服务

@Context
private HttpServletRequest httpRequest;

LOGGER.info("URL "+httpRequest.getRequestURI());

如果您的网址是http:// localhost:8080 / services / v1 / hello,则将提供/ services / v1 / hello

这可以通过从bean工厂检索bean定义并解析其元数据来实现。 例如:

  • 从您的应用程序上下文中检索beanFactory
  • 从bean工厂获取bean定义
  • 检索其元数据
  • 获取属性metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM