Java 从内部将url记录到Spring控制器

Java 从内部将url记录到Spring控制器,java,spring,model-view-controller,Java,Spring,Model View Controller,假设您有一个SpringMVC控制器,类似这样 @Controller public class RestController { @GetMapping(value = "/test") public @ResponseBody Test getTestData(...) { // console log path to controller: http://localhost:80/app/test return te

假设您有一个SpringMVC控制器,类似这样

@Controller
public class RestController {
 
    @GetMapping(value = "/test")
    public @ResponseBody Test getTestData(...) {

        // console log path to controller: http://localhost:80/app/test
        return testData;
    }
}
是否可以从控制器内部记录/打印指向它的url?在上面的示例中,输出类似于
https://localhost:80/app/test


使用servlet中的
.getRequestUrl
行为不正确。

您可以将UriComponentsBuilder作为参数注入,然后使用方法toUriString()。从文档中可以看出,它用于从当前请求的URI构建相对URI,这应该按照您的预期工作


“从servlet使用
.getRequestUrl
的行为不正确。”-实际和预期的输出是什么?不确定,您试图实现什么,您可以从请求获取服务器ip,您应该知道应用程序/上下文名称,并且您知道调用的api,为什么不能创建所需的URL?您可以将常量作为注释值传递。您可以基于几个常量构造URL,如(domain+restBasePath+instancePath(subPath))。。。Spring无法决定您的绝对路径,您应该比它更清楚相对路径。@Turing85 with
。getRequestUrl
我得到了指向jsp的链接,类似于
http://localhost:80/WEB-INF/view/../test.jsp
这不是我想要的。我想要指向控制器的链接。您的控制器在示例代码中没有设置任何路径。。。getMapping本身有一个映射。。。
@Controller
public class RestController {
...
    @GetMapping(value = "/test")
    public @ResponseBody Test getTestData(UriComponentsBuilder ucb, ...) {
        LOGGER.debug(ucb.toUriString());
        // console log path to controller: http://localhost:80/app/test
        return testData;
    }
...
}