类 ServletUriComponentsBuilder
- java.lang.Object
- org.springframework.web.util.UriComponentsBuilder
- org.springframework.web.servlet.support.ServletUriComponentsBuilder
- 所有已实现的接口:
Cloneable
public class ServletUriComponentsBuilder extends UriComponentsBuilder
UriComponentsBuilder with additional static factory methods to create links based on the current HttpServletRequest.Note: This class uses values from "Forwarded" (RFC 7239), "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers, if present, in order to reflect the client-originated protocol and address. Consider using the
ForwardedHeaderFilter
in order to choose from a central place whether to extract and use, or to discard such headers. See the Spring Framework reference for more on this filter.- 从以下版本开始:
- 3.1
- 作者:
- Rossen Stoyanchev
构造器概要
构造器 限定符 构造器 说明 protected
ServletUriComponentsBuilder()
Default constructor.protected
ServletUriComponentsBuilder(ServletUriComponentsBuilder other)
Create a deep copy of the given ServletUriComponentsBuilder.
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 ServletUriComponentsBuilder
cloneBuilder()
Clone thisUriComponentsBuilder
.static ServletUriComponentsBuilder
fromContextPath(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and context path of the given HttpServletRequest.static ServletUriComponentsBuilder
fromCurrentContextPath()
Same asfromContextPath(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.static ServletUriComponentsBuilder
fromCurrentRequest()
Same asfromRequest(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.static ServletUriComponentsBuilder
fromCurrentRequestUri()
Same asfromRequestUri(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.static ServletUriComponentsBuilder
fromCurrentServletMapping()
Same asfromServletMapping(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.static ServletUriComponentsBuilder
fromRequest(HttpServletRequest request)
Prepare a builder by copying the scheme, host, port, path, and query string of an HttpServletRequest.static ServletUriComponentsBuilder
fromRequestUri(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and path (but not the query) of the HttpServletRequest.static ServletUriComponentsBuilder
fromServletMapping(HttpServletRequest request)
Prepare a builder from the host, port, scheme, context path, and servlet mapping of the given HttpServletRequest.protected static HttpServletRequest
getCurrentRequest()
Obtain current request throughRequestContextHolder
.String
removePathExtension()
Remove any path extension from therequestURI
.从类继承的方法 org.springframework.web.util.UriComponentsBuilder
build, build, buildAndExpand, buildAndExpand, clone, fragment, fromHttpRequest, fromHttpUrl, fromOriginHeader, fromPath, fromUri, fromUriString, host, newInstance, path, pathSegment, port, port, query, queryParam, queryParams, replacePath, replaceQuery, replaceQueryParam, replaceQueryParams, scheme, schemeSpecificPart, toUriString, uri, uriComponents, userInfo
构造器详细资料
ServletUriComponentsBuilder
protected ServletUriComponentsBuilder()
Default constructor. Protected to prevent direct instantiation.
ServletUriComponentsBuilder
protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other)
Create a deep copy of the given ServletUriComponentsBuilder.- 参数:
other
- the other builder to copy from
方法详细资料
fromContextPath
public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and context path of the given HttpServletRequest.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromServletMapping
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request)
Prepare a builder from the host, port, scheme, context path, and servlet mapping of the given HttpServletRequest.If the servlet is mapped by name, e.g.
"/main/*"
, the path will end with "/main". If the servlet is mapped otherwise, e.g."/"
or"*.do"
, the result will be the same as if callingfromContextPath(HttpServletRequest)
.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromRequestUri
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request)
Prepare a builder from the host, port, scheme, and path (but not the query) of the HttpServletRequest.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromRequest
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request)
Prepare a builder by copying the scheme, host, port, path, and query string of an HttpServletRequest.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromCurrentContextPath
public static ServletUriComponentsBuilder fromCurrentContextPath()
Same asfromContextPath(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromCurrentServletMapping
public static ServletUriComponentsBuilder fromCurrentServletMapping()
Same asfromServletMapping(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromCurrentRequestUri
public static ServletUriComponentsBuilder fromCurrentRequestUri()
Same asfromRequestUri(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
fromCurrentRequest
public static ServletUriComponentsBuilder fromCurrentRequest()
Same asfromRequest(HttpServletRequest)
except the request is obtained throughRequestContextHolder
.Note: This method extracts values from "Forwarded" and "X-Forwarded-*" headers if found. See class-level docs.
As of 4.3.15, this method replaces the contextPath with the value of "X-Forwarded-Prefix" rather than prepending, thus aligning with
ForwardedHeaderFilter
.
getCurrentRequest
protected static HttpServletRequest getCurrentRequest()
Obtain current request throughRequestContextHolder
.
removePathExtension
public String removePathExtension()
Remove any path extension from therequestURI
. This method must be invoked before any calls toUriComponentsBuilder.path(String)
orUriComponentsBuilder.pathSegment(String...)
.GET http://www.foo.com/rest/books/6.json ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); String ext = builder.removePathExtension(); String uri = builder.path("/pages/1.{ext}").buildAndExpand(ext).toUriString(); assertEquals("http://www.foo.com/rest/books/6/pages/1.json", result);
- 返回:
- the removed path extension for possible re-use, or
null
- 从以下版本开始:
- 4.0
cloneBuilder
public ServletUriComponentsBuilder cloneBuilder()
从类复制的说明:UriComponentsBuilder
Clone thisUriComponentsBuilder
.- 覆盖:
cloneBuilder
在类中UriComponentsBuilder
- 返回:
- the cloned
UriComponentsBuilder
object