java - 如何在 Spring Boot RestController 中获取请求 URL

标签 java spring spring-boot

我正在尝试在 RestController 中获取请求 URL。 RestController 有多个用 @RequestMapping 注释的方法,用于不同的 URI,我想知道如何从 @RequestMapping 注释中获取绝对 URL。

@RestController
@RequestMapping(value = "/my/absolute/url/{urlid}/tests"
public class Test {
   @ResponseBody
   @RequestMapping(value "/",produces = "application/json")
   public String getURLValue(){
      //get URL value here which should be in this case, for instance if urlid      
       //is 1 in request then  "/my/absolute/url/1/tests"
      String test = getURL ?
      return test;
   }
} 

最佳答案

您可以尝试将 HttpServletRequest 类型的附加参数添加到 getUrlValue() 方法:

@RequestMapping(value ="/",produces = "application/json")
public String getURLValue(HttpServletRequest request){
    String test = request.getRequestURI();
    return test;
}

关于java - 如何在 Spring Boot RestController 中获取请求 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37710557/

相关文章:

Java - 获取字符频率,创建概率,然后生成伪随机字符

与 Microsoft Office 文档一起工作但不依赖于自动化的 Java 库

java.lang.IllegalThreadStateException : Thread already started error when using selectors with SocketChannel

java - ROME 如何确定提要中是否使用了自定义模块?

spring - 将 Kubernetes yaml 配置中定义的环境值传递给 java spring 配置文件

java - 通过(伪)直接调用该处理程序方法来测试 Spring Controller - 好还是坏?如何实现?

java - 在 Spring 安全中使用拦截 URL

spring - Controller 连接终止后的事务回滚(即客户端超时)

java - 运行测试时从项目根目录而不是模块加载 Spring Boot 文件系统资源/配置

java - 使用 Neo4Jconfiguration 连接 Neo4J 与 Spring boot 时发生未经授权的异常