【问题标题】:Is it possible to create REST endpoint in spring boot in runtime?是否可以在运行时的 Spring Boot 中创建 REST 端点?
【发布时间】:2023-04-02 16:31:01
【问题描述】:

我目前正在使用 Telegram 机器人,由于机器人消息中缺少将一个机器人的消息与另一个消息分开的信息,因此似乎可以在一个端点上提供一些电报机器人。新的机器人可以在运行时出现,所以我不能为每个机器人硬编码一些单独的端点。那么是否可以在运行时在spring boot中通过模板创建一个新的端点呢?

【问题讨论】:

  • 您可以执行类似路径变量(例如:{endpoint})的操作,该变量可以动态更改。创建一个规则来识别每个机器人并使用路径变量,例如 bot/{identifier} 或 bot/{identifier}/{other} 等等...stackoverflow.com/questions/26794198/…

标签: spring rest spring-boot telegram-bot


【解决方案1】:

没有。 DispatcherServlet 在作为根上下文的子上下文的 ApplicationContext 中初始化,因此您无法访问它。

拥有“动态”端点的一种方法是在请求映射中使用通配符。

@RequestMapping(value="/results/**", method=RequestMethod.GET)
public SomeResult handleResults(HttpServletRequest request) {
     String path = request. getRequestURI();
     if("asd".equals(path)){...}
}  

【讨论】:

  • 这比我的建议更好
  • request.getPathInfo() 返回null。可以使用request.getRequestURI()
猜你喜欢
  • 2022-06-30
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2016-12-25
  • 2019-01-03
  • 2015-12-25
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多