简体   繁体   中英

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

Lets say I inject an ApplicationContext into a bean. I can then discover all the beans annotated with RestController . How would I discover the base URL associated with said beans?

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. Not sure if below will help you....Try to inject HttpServletRequest in controller then from controller, pass it to required service

@Context
private HttpServletRequest httpRequest;

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

will give /services/v1/hello if your URL is http://localhost:8080/services/v1/hello

This could be possible by retrieving bean definition from bean factory and parsing its metadata. for instance:

  • retrieve beanFactory from your application Context
  • getting your bean definition from bean factory
  • retrieving its metadata
  • fetching attributes metadata.getAnnotationAttributes( "org.springframework.web.bind.annotation.RestController")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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