簡體   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