我有一个带有 @GetMapping 注释的其余 Controller ,用于将 HTTP 请求映射到特定处理程序方法:
@RestController
class Handler(){
@GetMapping(path = ["method"])
fun handlerMethod(){
//...
}
}
我还有电子邮件服务,我需要发送一些带有指向我的端点的链接的电子邮件(在这种情况下 http://host/method )
@Service
class EmailService(val config: Config){
fun sendEmail(){
// send "config.baseUrl/${getMethodPath()}"
}
fun getMethodPath(){
//todo
}
}
这里我从配置中获取基本 URL ( http://host )。有没有办法从 Handler 的 @GetMapping 获取 EmailService 中的路径属性,以构建到我的服务的链接?
最佳答案
可以通过RequestContextHolder
获取绑定(bind)到当前线程的请求。
((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest()
上面的代码将获取HttpServletRequest
,然后您可以使用[HttpServletRequest#getPathInfo()][1]
来获取path
网址的
关于java - 如何从其他服务获取@GetMapping内部的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55147599/