Top Description Fields Constructors Methods
org.springframework.web.servlet.support

public Class ServletUriComponentsBuilder

extends UriComponentsBuilder
Class Inheritance
Imports
jakarta.servlet.http.HttpServletRequest, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils, org.springframework.web.context.request.RequestAttributes, .RequestContextHolder, .ServletRequestAttributes, org.springframework.web.util.UriComponentsBuilder, .UriUtils, .UrlPathHelper

UriComponentsBuilder with additional static factory methods to create links based on the current HttpServletRequest.

Note

As of 5.1, methods in this class do not extract "Forwarded" and "X-Forwarded-*" headers that specify the client-originated address. Please, use ForwardedHeaderFilter, or similar from the underlying server, to extract and use such headers, or to discard them.

Author
Rossen Stoyanchev
Since
3.1

Field Summary

Modifier and TypeField and Description
private String

Constructor Summary

AccessConstructor and Description
protected
ServletUriComponentsBuilder()

Default constructor.

protected
ServletUriComponentsBuilder(ServletUriComponentsBuilder
the other builder to copy from
other
)

Create a deep copy of the given ServletUriComponentsBuilder.

Method Summary

Modifier and TypeMethod and Description
public ServletUriComponentsBuilder
public static ServletUriComponentsBuilder
fromContextPath(HttpServletRequest request)

Prepare a builder from the host, port, scheme, and context path of the given HttpServletRequest.

public static ServletUriComponentsBuilder
fromCurrentContextPath()

Same as fromContextPath(HttpServletRequest) except the request is obtained through RequestContextHolder.

public static ServletUriComponentsBuilder
fromCurrentRequest()

Same as fromRequest(HttpServletRequest) except the request is obtained through RequestContextHolder.

public static ServletUriComponentsBuilder
fromCurrentRequestUri()

Same as fromRequestUri(HttpServletRequest) except the request is obtained through RequestContextHolder.

public static ServletUriComponentsBuilder
public static ServletUriComponentsBuilder
fromRequest(HttpServletRequest request)

Prepare a builder by copying the scheme, host, port, path, and query string of an HttpServletRequest.

public static ServletUriComponentsBuilder
fromRequestUri(HttpServletRequest request)

Prepare a builder from the host, port, scheme, and path (but not the query) of the HttpServletRequest.

public 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 through RequestContextHolder.

private static ServletUriComponentsBuilder
initFromRequest(HttpServletRequest request)

Initialize a builder with a scheme, host,and port (but not path and query).

private void
public String

Returns:

the removed path extension for possible re-use, or null
removePathExtension
()

Remove any path extension from the requestURI.

Inherited from org.springframework.web.util.UriComponentsBuilder:
buildbuildbuildbuildbuildAndExpandbuildAndExpandcloneencodeencodefragmentfromHttpRequestfromHttpUrlfromOriginHeaderfromPathfromUrifromUriStringhostnewInstanceparseForwardedForpathpathSegmentportportqueryqueryParamqueryParamqueryParamIfPresentqueryParamsreplacePathreplaceQueryreplaceQueryParamreplaceQueryParamreplaceQueryParamsschemeschemeSpecificParttoUriStringuriuriComponentsuriVariablesuserInfo

Field Detail

originalPathback to summary
private String originalPath
Annotations
@Nullable

Constructor Detail

ServletUriComponentsBuilderback to summary
protected ServletUriComponentsBuilder()

Default constructor. Protected to prevent direct instantiation.

See Also
fromContextPath(HttpServletRequest), fromServletMapping(HttpServletRequest), fromRequest(HttpServletRequest), fromCurrentContextPath(), fromCurrentServletMapping(), fromCurrentRequest()
ServletUriComponentsBuilderback to summary
protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other)

Create a deep copy of the given ServletUriComponentsBuilder.

Parameters
other:ServletUriComponentsBuilder

the other builder to copy from

Method Detail

cloneBuilderback to summary
public ServletUriComponentsBuilder cloneBuilder()

Overrides org.springframework.web.util.UriComponentsBuilder.cloneBuilder.

Doc from org.springframework.web.util.UriComponentsBuilder.cloneBuilder.

Clone this UriComponentsBuilder.

Returns:ServletUriComponentsBuilder

the cloned UriComponentsBuilder object

Annotations
@Override
fromContextPathback to summary
public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request)

Prepare a builder from the host, port, scheme, and context path of the given HttpServletRequest.

fromCurrentContextPathback to summary
public static ServletUriComponentsBuilder fromCurrentContextPath()

Same as fromContextPath(HttpServletRequest) except the request is obtained through RequestContextHolder.

fromCurrentRequestback to summary
public static ServletUriComponentsBuilder fromCurrentRequest()

Same as fromRequest(HttpServletRequest) except the request is obtained through RequestContextHolder.

fromCurrentRequestUriback to summary
public static ServletUriComponentsBuilder fromCurrentRequestUri()

Same as fromRequestUri(HttpServletRequest) except the request is obtained through RequestContextHolder.

fromCurrentServletMappingback to summary
public static ServletUriComponentsBuilder fromCurrentServletMapping()

Same as fromServletMapping(HttpServletRequest) except the request is obtained through RequestContextHolder.

fromRequestback to summary
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request)

Prepare a builder by copying the scheme, host, port, path, and query string of an HttpServletRequest.

fromRequestUriback to summary
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request)

Prepare a builder from the host, port, scheme, and path (but not the query) of the HttpServletRequest.

fromServletMappingback to summary
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 calling fromContextPath(HttpServletRequest).

getCurrentRequestback to summary
protected static HttpServletRequest getCurrentRequest()

Obtain current request through RequestContextHolder.

initFromRequestback to summary
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request)

Initialize a builder with a scheme, host,and port (but not path and query).

initPathback to summary
private void initPath(String path)
removePathExtensionback to summary
public String removePathExtension()

Remove any path extension from the requestURI. This method must be invoked before any calls to path(String) or pathSegment(String...).

GET http://www.foo.example/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.example/rest/books/6/pages/1.json", result);
Returns:String

the removed path extension for possible re-use, or null

Annotations
@Nullable
Since
4.0