org.springframework.web.servlet.support

## Class ServletUriComponentsBuilder

- java.lang.Object
- - org.springframework.web.util.UriComponentsBuilder
  - - org.springframework.web.servlet.support.ServletUriComponentsBuilder

-     public class ServletUriComponentsBuilder
      extends UriComponentsBuilder

  A UriComponentsBuilder that extracts information from an HttpServletRequest.
  Since:
    3.1
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `ServletUriComponentsBuilder()` Default constructor.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static ServletUriComponentsBuilder` | `fromContextPath(HttpServletRequest request)` Prepare a builder from the host, port, scheme, and context path of an HttpServletRequest.
    `static ServletUriComponentsBuilder` | `fromCurrentContextPath()` Same as `fromContextPath(HttpServletRequest)` except the request is obtained through `RequestContextHolder`.
    `static ServletUriComponentsBuilder` | `fromCurrentRequest()` Same as `fromRequest(HttpServletRequest)` except the request is obtained through `RequestContextHolder`.
    `static ServletUriComponentsBuilder` | `fromCurrentRequestUri()` Same as `fromRequestUri(HttpServletRequest)` except the request is obtained through `RequestContextHolder`.
    `static ServletUriComponentsBuilder` | `fromCurrentServletMapping()` Same as `fromServletMapping(HttpServletRequest)` except the request is obtained through `RequestContextHolder`.
    `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 of an HttpServletRequest.
    `static ServletUriComponentsBuilder` | `fromServletMapping(HttpServletRequest request)` Prepare a builder from the host, port, scheme, context path, and servlet mapping of an HttpServletRequest.
    `protected static HttpServletRequest` | `getCurrentRequest()` Obtain the request through `RequestContextHolder`.
    `String` | `removePathExtension()` Removes any path extension from the `requestURI`.

    - ### Methods inherited from class org.springframework.web.util.UriComponentsBuilder

      `build, build, buildAndExpand, buildAndExpand, fragment, fromHttpUrl, fromPath, fromUri, fromUriString, host, newInstance, path, pathSegment, port, query, queryParam, queryParams, replacePath, replaceQuery, replaceQueryParam, scheme, schemeSpecificPart, uri, uriComponents, userInfo`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ServletUriComponentsBuilder

          protected ServletUriComponentsBuilder()

      Default constructor. Protected to prevent direct instantiation.
      See Also:
        `fromContextPath(HttpServletRequest)`, `fromServletMapping(HttpServletRequest)`, `fromRequest(HttpServletRequest)`, `fromCurrentContextPath()`, `fromCurrentServletMapping()`, `fromCurrentRequest()`

  - ### Method Detail

    - #### fromContextPath

          public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request)

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

    - #### fromServletMapping

          public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request)

      Prepare a builder from the host, port, scheme, context path, and servlet mapping of an HttpServletRequest. The results may vary depending on the type of servlet mapping used.

      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)`.

    - #### fromRequestUri

          public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request)

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

    - #### fromRequest

          public static ServletUriComponentsBuilder fromRequest(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 HttpServletRequest getCurrentRequest()

      Obtain the request through `RequestContextHolder`.

    - #### removePathExtension

          public String removePathExtension()

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

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

      Returns:
        the removed path extension for possible re-use, or `null`
      Since:
        4.0