Class ServletUriComponentsBuilder

java.lang.Object
org.springframework.web.util.UriComponentsBuilder
org.springframework.web.servlet.support.ServletUriComponentsBuilder
All Implemented Interfaces:
Cloneable, org.springframework.web.util.UriBuilder

public class ServletUriComponentsBuilder extends org.springframework.web.util.UriComponentsBuilder
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.

Since:
3.1
Author:
Rossen Stoyanchev
  • Constructor Details

  • Method Details

    • fromContextPath

      public static ServletUriComponentsBuilder fromContextPath(jakarta.servlet.http.HttpServletRequest request)
      Prepare a builder from the host, port, scheme, and context path of the given HttpServletRequest.
    • fromServletMapping

      public static ServletUriComponentsBuilder fromServletMapping(jakarta.servlet.http.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, for example, "/main/*", the path will end with "/main". If the servlet is mapped otherwise, for example, "/" or "*.do", the result will be the same as if calling fromContextPath(HttpServletRequest).

    • fromRequestUri

      public static ServletUriComponentsBuilder fromRequestUri(jakarta.servlet.http.HttpServletRequest request)
      Prepare a builder from the host, port, scheme, and path (but not the query) of the HttpServletRequest.
    • fromRequest

      public static ServletUriComponentsBuilder fromRequest(jakarta.servlet.http.HttpServletRequest request)
      Prepare a builder by copying the scheme, host, port, path, and query string of an HttpServletRequest.
    • fromCurrentContextPath

      public static ServletUriComponentsBuilder fromCurrentContextPath()
      Same as fromContextPath(HttpServletRequest) except the request is obtained through RequestContextHolder.
    • fromCurrentServletMapping

      public static ServletUriComponentsBuilder fromCurrentServletMapping()
      Same as fromServletMapping(HttpServletRequest) except the request is obtained through RequestContextHolder.
    • fromCurrentRequestUri

      public static ServletUriComponentsBuilder fromCurrentRequestUri()
      Same as fromRequestUri(HttpServletRequest) except the request is obtained through RequestContextHolder.
    • fromCurrentRequest

      public static ServletUriComponentsBuilder fromCurrentRequest()
      Same as fromRequest(HttpServletRequest) except the request is obtained through RequestContextHolder.
    • getCurrentRequest

      protected static jakarta.servlet.http.HttpServletRequest getCurrentRequest()
      Obtain current request through RequestContextHolder.
    • removePathExtension

      public @Nullable String removePathExtension()
      Remove any path extension from the requestURI. This method must be invoked before any calls to UriComponentsBuilder.path(String) or UriComponentsBuilder.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:
      the removed path extension for possible re-use, or null
      Since:
      4.0
    • cloneBuilder

      public ServletUriComponentsBuilder cloneBuilder()
      Overrides:
      cloneBuilder in class org.springframework.web.util.UriComponentsBuilder