接口 ServerRequest


  • public interface ServerRequest
    Represents a server-side HTTP request, as handled by a HandlerFunction. Access to headers and body is offered by ServerRequest.Headers and body(Class), respectively.
    从以下版本开始:
    5.2
    作者:
    Arjen Poutsma
    • 方法详细资料

      • method

        @Nullable
        default HttpMethod method()
        Get the HTTP method.
        返回:
        the HTTP method as an HttpMethod enum value, or null if not resolvable (e.g. in case of a non-standard HTTP method)
      • methodName

        String methodName()
        Get the name of the HTTP method.
        返回:
        the HTTP method as a String
      • uri

        URI uri()
        Get the request URI.
      • uriBuilder

        UriBuilder uriBuilder()
        Get a UriBuilderComponents from the URI associated with this ServerRequest.
        返回:
        a URI builder
      • attribute

        default Optional<Objectattribute​(String name)
        Get the request attribute value if present.
        参数:
        name - the attribute name
        返回:
        the attribute value
      • pathVariable

        default String pathVariable​(String name)
        Get the path variable with the given name, if present.
        参数:
        name - the variable name
        返回:
        the variable value
        抛出:
        IllegalArgumentException - if there is no path variable with the given name
      • session

        HttpSession session()
        Get the web session for this request. Always guaranteed to return an instance either matching to the session id requested by the client, or with a new session id either because the client did not specify one or because the underlying session had expired. Use of this method does not automatically create a session.
      • checkNotModified

        default Optional<ServerResponsecheckNotModified​(Instant lastModified)
        Check whether the requested resource has been modified given the supplied last-modified timestamp (as determined by the application). If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.

        Typical usage:

         public ServerResponse myHandleMethod(ServerRequest request) {
           Instant lastModified = // application-specific calculation
                 return request.checkNotModified(lastModified)
                   .orElseGet(() -> {
                     // further request processing, actually building content
                         return ServerResponse.ok().body(...);
                   });
         }

        This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.

        Note: you can use either this #checkNotModified(Instant) method; or checkNotModified(String). If you want enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use checkNotModified(Instant, String).

        参数:
        lastModified - the last-modified timestamp that the application determined for the underlying resource
        返回:
        a corresponding response if the request qualifies as not modified, or an empty result otherwise.
        从以下版本开始:
        5.2.5
      • checkNotModified

        default Optional<ServerResponsecheckNotModified​(String etag)
        Check whether the requested resource has been modified given the supplied ETag (entity tag), as determined by the application. If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.

        Typical usage:

         public ServerResponse myHandleMethod(ServerRequest request) {
           String eTag = // application-specific calculation
                 return request.checkNotModified(eTag)
                   .orElseGet(() -> {
                     // further request processing, actually building content
                         return ServerResponse.ok().body(...);
                   });
         }

        This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.

        Note: you can use either this checkNotModified(Instant) method; or #checkNotModified(String). If you want enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use checkNotModified(Instant, String).

        参数:
        etag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
        返回:
        a corresponding response if the request qualifies as not modified, or an empty result otherwise.
        从以下版本开始:
        5.2.5
      • checkNotModified

        default Optional<ServerResponsecheckNotModified​(Instant lastModified,
                                                          String etag)
        Check whether the requested resource has been modified given the supplied ETag (entity tag) and last-modified timestamp, as determined by the application. If not modified, this method returns a response with corresponding status code and headers, otherwise an empty result.

        Typical usage:

         public ServerResponse myHandleMethod(ServerRequest request) {
           Instant lastModified = // application-specific calculation
           String eTag = // application-specific calculation
                 return request.checkNotModified(lastModified, eTag)
                   .orElseGet(() -> {
                     // further request processing, actually building content
                         return ServerResponse.ok().body(...);
                   });
         }

        This method works with conditional GET/HEAD requests, but also with conditional POST/PUT/DELETE requests.

        参数:
        lastModified - the last-modified timestamp that the application determined for the underlying resource
        etag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
        返回:
        a corresponding response if the request qualifies as not modified, or an empty result otherwise.
        从以下版本开始:
        5.2.5
      • create

        static ServerRequest create​(HttpServletRequest servletRequest,
                                    List<HttpMessageConverter<?>> messageReaders)
        Create a new ServerRequest based on the given HttpServletRequest and message converters.
        参数:
        servletRequest - the request
        messageReaders - the message readers
        返回:
        the created ServerRequest
      • from

        static ServerRequest.Builder from​(ServerRequest other)
        Create a builder with the status, headers, and cookies of the given request.
        参数:
        other - the response to copy the status, headers, and cookies from
        返回:
        the created builder