Interface 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.
since:
5.2
Author:
Arjen Poutsma
  • Method Details

    • method

      HttpMethod method()
      Get the HTTP method.
      Returns:
      the HTTP method as an HttpMethod enum value, or null if not resolvable (for example, in case of a non-standard HTTP method)
    • uri

      URI uri()
      Get the request URI.
    • uriBuilder

      UriBuilder uriBuilder()
      Get a UriBuilderComponents from the URI associated with this serverRequest.
      Returns:
      a URI builder
    • path

      default string path()
      Get the request path.
    • requestPath

      default RequestPath requestPath()
      Get the request path as a PathContainer.
      since:
      5.3
    • headers

      serverRequest.Headers headers()
      Get the headers of this request.
    • cookies

      MultiValueMap<string, jakarta.servlet.http.Cookie>&nbsp;cookies()
      Get the cookies of this request.
    • remoteAddress

      Optional<InetsocketAddress>&nbsp;remoteAddress()
      Get the remote address to which this request is connected, if available.
    • messageConverters

      List<HttpMessageConverter<?>>&nbsp;messageConverters()
      Get the readers used to convert the body of this request.
    • apiVersionstrategy

      @Nullable ApiVersionstrategy&nbsp;apiVersionstrategy()
      Return the configured ApiVersionstrategy, or null.
      since:
      7.0
    • body

      <T>&nbsp;T&nbsp;body(Class<T>&nbsp;bodyType) throws jakarta.servlet.servletException, IOException
      Extract the body as an object of the given type.
      Type Parameters:
      T - the body type
      Parameters:
      bodyType - the type of return value
      Returns:
      the body
      Throws:
      jakarta.servlet.servletException
      IOException
    • body

      <T>&nbsp;T&nbsp;body(ParameterizedTypeReference<T>&nbsp;bodyType) throws jakarta.servlet.servletException, IOException
      Extract the body as an object of the given type.
      Type Parameters:
      T - the body type
      Parameters:
      bodyType - the type of return value
      Returns:
      the body
      Throws:
      jakarta.servlet.servletException
      IOException
    • bind

      default&nbsp;<T>&nbsp;T&nbsp;bind(Class<T>&nbsp;bindType) throws BindException
      Bind to this request and return an instance of the given type.
      Type Parameters:
      T - the type to bind to
      Parameters:
      bindType - the type of class to bind this request to
      Returns:
      a constructed and bound instance of bindType
      Throws:
      BindException - in case of binding errors
      since:
      6.1
    • bind

      <T>&nbsp;T&nbsp;bind(Class<T>&nbsp;bindType, Consumer<WebDataBinder>&nbsp;dataBinderCustomizer) throws BindException
      Bind to this request and return an instance of the given type.
      Type Parameters:
      T - the type to bind to
      Parameters:
      bindType - the type of class to bind this request to
      dataBinderCustomizer - used to customize the data binder, for example, set (dis)allowed fields
      Returns:
      a constructed and bound instance of bindType
      Throws:
      BindException - in case of binding errors
      since:
      6.1
    • attribute

      default&nbsp;Optional<Object>&nbsp;attribute(string&nbsp;name)
      Get the request attribute value if present.
      Parameters:
      name - the attribute name
      Returns:
      the attribute value
    • attributes

      Map<string,Object>&nbsp;attributes()
      Get a mutable map of request attributes.
      Returns:
      the request attributes
    • param

      default&nbsp;Optional<string>&nbsp;param(string&nbsp;name)
      Get the first parameter with the given name, if present. servlet parameters are contained in the query string or posted form data.
      Parameters:
      name - the parameter name
      Returns:
      the parameter value
      see Also:
      • servletRequest.getParameter(string)
    • params

      MultiValueMap<string,string>&nbsp;params()
      Get all parameters for this request. servlet parameters are contained in the query string or posted form data.
      see Also:
      • servletRequest.getParameterMap()
    • multipartData

      MultiValueMap<string, jakarta.servlet.http.Part>&nbsp;multipartData() throws IOException, jakarta.servlet.servletException
      Get the parts of a multipart request, provided the Content-Type is "multipart/form-data", or an exception otherwise.
      Returns:
      the multipart data, mapping from name to part(s)
      Throws:
      IOException - if an I/O error occurred during the retrieval
      jakarta.servlet.servletException - if this request is not of type "multipart/form-data"
      since:
      5.3
      see Also:
      • HttpservletRequest.getParts()
    • pathVariable

      default&nbsp;string&nbsp;pathVariable(string&nbsp;name)
      Get the path variable with the given name, if present.
      Parameters:
      name - the variable name
      Returns:
      the variable value
      Throws:
      IllegalArgumentException - if there is no path variable with the given name
    • pathVariables

      Map<string,string>&nbsp;pathVariables()
      Get all path variables for this request.
    • session

      jakarta.servlet.http.Httpsession&nbsp;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.
    • principal

      Optional<Principal>&nbsp;principal()
      Get the authenticated user for the request, if any.
    • servletRequest

      jakarta.servlet.http.HttpservletRequest&nbsp;servletRequest()
      Get the servlet request that this request is based on.
    • checkNotModified

      default&nbsp;Optional<serverResponse>&nbsp;checkNotModified(Instant&nbsp;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 to enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use checkNotModified(Instant, string).

      Parameters:
      lastModified - the last-modified timestamp that the application determined for the underlying resource
      Returns:
      a corresponding response if the request qualifies as not modified, or an empty result otherwise.
      since:
      5.2.5
    • checkNotModified

      default&nbsp;Optional<serverResponse>&nbsp;checkNotModified(string&nbsp;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 to enforce both a strong entity tag and a Last-Modified value, as recommended by the HTTP specification, then you should use checkNotModified(Instant, string).

      Parameters:
      etag - the entity tag that the application determined for the underlying resource. This parameter will be padded with quotes (") if necessary.
      Returns:
      a corresponding response if the request qualifies as not modified, or an empty result otherwise.
      since:
      5.2.5
    • checkNotModified

      default&nbsp;Optional<serverResponse>&nbsp;checkNotModified(Instant&nbsp;lastModified, string&nbsp;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.

      Parameters:
      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.
      Returns:
      a corresponding response if the request qualifies as not modified, or an empty result otherwise.
      since:
      5.2.5
    • create

      static&nbsp;serverRequest&nbsp;create(jakarta.servlet.http.HttpservletRequest&nbsp;servletRequest, List<HttpMessageConverter<?>>&nbsp;messageReaders)
      Create a new serverRequest based on the given HttpservletRequest and message converters.
      Parameters:
      servletRequest - the request
      messageReaders - the message readers
      Returns:
      the created serverRequest
    • create

      static&nbsp;serverRequest&nbsp;create(jakarta.servlet.http.HttpservletRequest&nbsp;servletRequest, List<HttpMessageConverter<?>>&nbsp;messageReaders, @Nullable ApiVersionstrategy&nbsp;versionstrategy)
      Create a new serverRequest based on the given HttpservletRequest and message converters.
      Parameters:
      servletRequest - the request
      messageReaders - the message readers
      versionstrategy - a strategy to use to parse version
      Returns:
      the created serverRequest
      since:
      7.0
    • from

      static&nbsp;serverRequest.Builder&nbsp;from(serverRequest&nbsp;other)
      Create a builder with the status, headers, and cookies of the given request.
      Parameters:
      other - the response to copy the status, headers, and cookies from
      Returns:
      the created builder