类 UriComponentsBuilder

    • 方法详细资料

      • fromPath

        public static UriComponentsBuilder fromPath​(String path)
        Create a builder that is initialized with the given path.
        参数:
        path - the path to initialize with
        返回:
        the new UriComponentsBuilder
      • fromUri

        public static UriComponentsBuilder fromUri​(URI uri)
        Create a builder that is initialized from the given URI.

        Note: the components in the resulting builder will be in fully encoded (raw) form and further changes must also supply values that are fully encoded, for example via methods in UriUtils. In addition please use build(boolean) with a value of "true" to build the UriComponents instance in order to indicate that the components are encoded.

        参数:
        uri - the URI to initialize with
        返回:
        the new UriComponentsBuilder
      • fromUriString

        public static UriComponentsBuilder fromUriString​(String uri)
        Create a builder that is initialized with the given URI string.

        Note: The presence of reserved characters can prevent correct parsing of the URI string. For example if a query parameter contains '=' or '&' characters, the query string cannot be parsed unambiguously. Such values should be substituted for URI variables to enable correct parsing:

         String uriString = "/hotels/42?filter={value}";
         UriComponentsBuilder.fromUriString(uriString).buildAndExpand("hot&cold");
         
        参数:
        uri - the URI string to initialize with
        返回:
        the new UriComponentsBuilder
      • fromHttpUrl

        public static UriComponentsBuilder fromHttpUrl​(String httpUrl)
        Create a URI components builder from the given HTTP URL String.

        Note: The presence of reserved characters can prevent correct parsing of the URI string. For example if a query parameter contains '=' or '&' characters, the query string cannot be parsed unambiguously. Such values should be substituted for URI variables to enable correct parsing:

         String urlString = "https://example.com/hotels/42?filter={value}";
         UriComponentsBuilder.fromHttpUrl(urlString).buildAndExpand("hot&cold");
         
        参数:
        httpUrl - the source URI
        返回:
        the URI components of the URI
      • fromHttpRequest

        public static UriComponentsBuilder fromHttpRequest​(HttpRequest request)
        Create a new UriComponents object from the URI associated with the given HttpRequest while also overlaying with values from the headers "Forwarded" (RFC 7239), or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if "Forwarded" is not found.

        Note: this method uses values from forwarded 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.

        参数:
        request - the source request
        返回:
        the URI components of the URI
        从以下版本开始:
        4.1.5
      • build

        public UriComponents build()
        Build a UriComponents instance from the various components contained in this builder.
        返回:
        the URI components
      • build

        public UriComponents build​(boolean encoded)
        Variant of build() to create a UriComponents instance when components are already fully encoded. This is useful for example if the builder was created via fromUri(URI).
        参数:
        encoded - whether the components in this builder are already encoded
        返回:
        the URI components
        抛出:
        IllegalArgumentException - if any of the components contain illegal characters that should have been encoded.
      • buildAndExpand

        public UriComponents buildAndExpand​(Map<String,​?> uriVariables)
        Build a UriComponents instance and replaces URI template variables with the values from a map. This is a shortcut method which combines calls to build() and then UriComponents.expand(Map).
        参数:
        uriVariables - the map of URI variables
        返回:
        the URI components with expanded values
      • buildAndExpand

        public UriComponents buildAndExpand​(Object... uriVariableValues)
        Build a UriComponents instance and replaces URI template variables with the values from an array. This is a shortcut method which combines calls to build() and then UriComponents.expand(Object...).
        参数:
        uriVariableValues - URI variable values
        返回:
        the URI components with expanded values
      • uri

        public UriComponentsBuilder uri​(URI uri)
        Initialize components of this builder from components of the given URI.
        参数:
        uri - the URI
        返回:
        this UriComponentsBuilder
      • uriComponents

        public UriComponentsBuilder uriComponents​(UriComponents uriComponents)
        Set or append individual URI components of this builder from the values of the given UriComponents instance.

        For the semantics of each component (i.e. set vs append) check the builder methods on this class. For example host(String) sets while path(String) appends.

        参数:
        uriComponents - the UriComponents to copy from
        返回:
        this UriComponentsBuilder
      • scheme

        public UriComponentsBuilder scheme​(String scheme)
        Set the URI scheme. The given scheme may contain URI template variables, and may also be null to clear the scheme of this builder.
        参数:
        scheme - the URI scheme
        返回:
        this UriComponentsBuilder
      • userInfo

        public UriComponentsBuilder userInfo​(String userInfo)
        Set the URI user info. The given user info may contain URI template variables, and may also be null to clear the user info of this builder.
        参数:
        userInfo - the URI user info
        返回:
        this UriComponentsBuilder
      • host

        public UriComponentsBuilder host​(String host)
        Set the URI host. The given host may contain URI template variables, and may also be null to clear the host of this builder.
        参数:
        host - the URI host
        返回:
        this UriComponentsBuilder
      • port

        public UriComponentsBuilder port​(int port)
        Set the URI port. Passing -1 will clear the port of this builder.
        参数:
        port - the URI port
        返回:
        this UriComponentsBuilder
      • port

        public UriComponentsBuilder port​(String port)
        Set the URI port. Use this method only when the port needs to be parameterized with a URI variable. Otherwise use port(int). Passing null will clear the port of this builder.
        参数:
        port - the URI port
        返回:
        this UriComponentsBuilder
      • path

        public UriComponentsBuilder path​(String path)
        Append the given path to the existing path of this builder. The given path may contain URI template variables.
        参数:
        path - the URI path
        返回:
        this UriComponentsBuilder
      • replacePath

        public UriComponentsBuilder replacePath​(String path)
        Set the path of this builder overriding all existing path and path segment values.
        参数:
        path - the URI path (a null value results in an empty path)
        返回:
        this UriComponentsBuilder
      • query

        public UriComponentsBuilder query​(String query)
        Append the given query to the existing query of this builder. The given query may contain URI template variables.

        Note: The presence of reserved characters can prevent correct parsing of the URI string. For example if a query parameter contains '=' or '&' characters, the query string cannot be parsed unambiguously. Such values should be substituted for URI variables to enable correct parsing:

         UriComponentsBuilder.fromUriString("/hotels/42")
                .query("filter={value}")
                .buildAndExpand("hot&cold");
         
        参数:
        query - the query string
        返回:
        this UriComponentsBuilder
      • replaceQuery

        public UriComponentsBuilder replaceQuery​(String query)
        Set the query of this builder overriding all existing query parameters.
        参数:
        query - the query string; a null value removes all query parameters.
        返回:
        this UriComponentsBuilder
      • queryParam

        public UriComponentsBuilder queryParam​(String name,
                                               Object... values)
        Append the given query parameter to the existing query parameters. The given name or any of the values may contain URI template variables. If no values are given, the resulting URI will contain the query parameter name only (i.e. ?foo instead of ?foo=bar).
        参数:
        name - the query parameter name
        values - the query parameter values
        返回:
        this UriComponentsBuilder
      • replaceQueryParam

        public UriComponentsBuilder replaceQueryParam​(String name,
                                                      Object... values)
        Set the query parameter values overriding all existing query values for the same parameter. If no values are given, the query parameter is removed.
        参数:
        name - the query parameter name
        values - the query parameter values
        返回:
        this UriComponentsBuilder
      • fragment

        public UriComponentsBuilder fragment​(String fragment)
        Set the URI fragment. The given fragment may contain URI template variables, and may also be null to clear the fragment of this builder.
        参数:
        fragment - the URI fragment
        返回:
        this UriComponentsBuilder
      • cloneBuilder

        public UriComponentsBuilder cloneBuilder()
        Clone this UriComponentsBuilder.
        返回:
        the cloned UriComponentsBuilder object
        从以下版本开始:
        4.2.7