gpt4 book ai didi

java - 如何在非servlet环境中使用UriComponentsBuilder?

转载 作者:行者123 更新时间:2023-12-01 09:16:14 26 4
gpt4 key购买 nike

文档指出,如果我不在 servlet 调用线程内,但仍想使用 UriComponentsBuilder (例如在批量导入中),我可以使用 ServletUriComponentsBuilder.fromCurrentContextPath ().

@参见https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-uri-building

我从文档中尝试了以下操作:

public String createUrl() {
UriComponentsBuilder base = ServletUriComponentsBuilder.fromCurrentContextPath().path("/en");
MvcUriComponentsBuilder builder = MvcUriComponentsBuilder.relativeTo(base);
builder.withMethodCall(on(BookingController.class).getBooking(21)).buildAndExpand(42);

URI uri = uriComponents.encode().toUri();
return uri.toString();
}

用于测试非 servlet 调用:

@PostConstruct
public void init() {
builder.createUrl();
}

但总是遇到异常:

Caused by: java.lang.IllegalStateException: Could not find current request via RequestContextHolder
at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.servlet.support.ServletUriComponentsBuilder.getCurrentRequest(ServletUriComponentsBuilder.java:190) ~[spring-webmvc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentContextPath(ServletUriComponentsBuilder.java:158) ~[spring-webmvc-4.3.4.RELEASE.jar:4.3.4.RELEASE]

那么这里可能出了什么问题?

最佳答案

您提到的文档明确指出静态方法用于 Servlet 环境:

In a Servlet environment the ServletUriComponentsBuilder sub-class provides static factory methods [...]

因此,如果您不在上述 Servlet 环境中,则需要使用普通的 UriComponentsBuilder:

UriComponentsBuilder.newInstance().scheme("http").host("example.com")

这适合您的代码

public String createUrl() {
UriComponentsBuilder base = UriComponentsBuilder.newInstance().scheme("http").host("example.com").path("/en");
MvcUriComponentsBuilder builder = MvcUriComponentsBuilder.relativeTo(base);
builder.withMethodCall(on(BookingController.class).getBooking(21)).buildAndExpand(42);

URI uri = uriComponents.encode().toUri();
return uri.toString();
}

这背后的原因是,在 servlet 环境中,您可以通过查询 Servlet/ServletContext 等来访问方案、主机和上下文路径。在 Web 应用程序外部运行,此信息(方案、主机、上下文路径)需要来自其他地方。

关于java - 如何在非servlet环境中使用UriComponentsBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40527179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com