[英]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: 例如:
metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController")
获取属性metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.