简体   繁体   English

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

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

Lets say I inject an ApplicationContext into a bean. 可以说我将ApplicationContext注入到bean中。 I can then discover all the beans annotated with RestController . 然后,我可以发现所有用RestController注释的bean。 How would I discover the base URL associated with said beans? 我将如何发现与所述bean相关联的基本URL?

Example: 例:

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

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

As per standard, Base URL (Context root) needs to setup in configuration file. 按照标准,基本URL(上下文根)需要在配置文件中设置。 Not sure if below will help you....Try to inject HttpServletRequest in controller then from controller, pass it to required service 不确定以下内容是否对您有帮助。...尝试在控制器中注入HttpServletRequest,然后从控制器中将其传递到所需的服务

@Context
private HttpServletRequest httpRequest;

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

will give /services/v1/hello if your URL is http://localhost:8080/services/v1/hello 如果您的网址是http:// localhost:8080 / services / v1 / hello,则将提供/ services / v1 / hello

This could be possible by retrieving bean definition from bean factory and parsing its metadata. 这可以通过从bean工厂检索bean定义并解析其元数据来实现。 for instance: 例如:

  • retrieve beanFactory from your application Context 从您的应用程序上下文中检索beanFactory
  • getting your bean definition from bean factory 从bean工厂获取bean定义
  • retrieving its metadata 检索其元数据
  • fetching attributes metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController") 获取属性metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController")

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

相关问题 如何在 Spring Boot RestController 中获取请求 URL - How to get request URL in Spring Boot RestController 如何从 Validated Spring RestController 获取多个缺失请求参数的详细信息? - How do I get the details of multiple missing request parameters from a Validated Spring RestController? 如何在Spring @RestController中注入完整的请求路径? - How to inject the full request path in Spring @RestController? 如何将URL作为@PathVariable传递给Spring RestController? - How to pass url as @PathVariable to spring RestController? Spring @RestController没有使用Kotlin映射URL - Spring @RestController is not mapping URL with Kotlin Spring Boot-如何在Spring RestController中的映射中获取所有请求参数? - Spring Boot - How to get all request params in a map in Spring RestController? 如何获取Spring @RestController以JSON格式而不是www-form-urlencoded接受参数? - How can I get a Spring @RestController to accept parameters in JSON format rather than www-form-urlencoded? Spring Boot - 如何获取请求的基本URL - Spring Boot - how do I get the base url of a request 如何从 Spring 应用程序中的 URL 获取延续映射? - How do I get the continuation mapping from the URL in a Spring application? Spring 3 web请求拦截器 - 我如何获得BindingResult? - Spring 3 web request interceptor - how do I get BindingResult?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM