Relative Servlet Requests
你可以使用 ServletUriComponentsBuilder 来创建相对于当前请求的 URI,如下例所示:
HttpServletRequest request = ...// 会利用 request 中的 host, scheme, port, path and query string... 来构建 URIServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromRequest(request).replaceQueryParam("accountId", "{id}").build().expand("123").encode();
你可以创建相对于上下文路径的 URI,如下例所示:
// Re-uses host, port and context path...ServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
你可以创建相对于 Servlet 的 URI(例如,/main/*),如下例所示:
// Re-uses host, port, context path, and Servlet prefix...ServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromServletMapping(request).path("/accounts").build()
:::info 从 5.1 开始,ServletUriComponentsBuilder 忽略了来自 Forwarded 和 X-Forwarded-* 头的信息,这些头指定了客户端来源的地址。考虑使用ForwardedHeaderFilter 来提取和使用或丢弃这些头信息。 :::
