Class HttpStatus


  • public final class HttpStatus
    extends java.lang.Object

    An event handler method may return an instance of this class to send an specific HTTP status code to the client. It also supports providing a string to be used as the response body and extra HTTP headers to be set. This class also provides some utility static methods for creating instances for specific HTTP statuses and a fluent API for setting additional information on them.

    For returning binary content and/or adding a response header more than once and/or adding a response header without overwriting existing ones, implementing a StreamResponse is the most probable better choice.

    Since:
    5.8.0
    • Constructor Detail

      • HttpStatus

        public HttpStatus​(int statusCode)
        Creates an object with a given status code and no response body.
      • HttpStatus

        public HttpStatus​(int statusCode,
                          java.lang.String responseBody)
        Creates an object with a given status code, response body and text/plain MIME content type.
      • HttpStatus

        public HttpStatus​(int statusCode,
                          java.lang.String responseBody,
                          java.lang.String contentType)
        Creates an object with a given status code, response body and MIME content type.
    • Method Detail

      • ok

        public static HttpStatus ok()
        Creates an instance with status code 200 OK.
      • created

        public static HttpStatus created()
        Creates an instance with status code 201 Created.
      • accepted

        public static HttpStatus accepted()
        Creates an instance with status code 202 Accepted.
      • notFound

        public static HttpStatus notFound()
        Creates an instance with status code 404 Not Found.
      • forbidden

        public static HttpStatus forbidden()
        Creates an instance with status code 403 Forbidden.
      • badRequest

        public static HttpStatus badRequest()
        Creates an instance with status code 400 Bad Request.
      • unauthorized

        public static HttpStatus unauthorized()
        Creates an instance with status code 401 Unauthorized.
      • seeOther

        public static HttpStatus seeOther​(java.lang.String location)
        Creates an instance with status code 303 See Other.
        Parameters:
        location - the value of the Location header.
      • seeOther

        public static HttpStatus seeOther​(Link location)
        Creates an instance with status code 303 See Also.
        Parameters:
        location - the value of the Location header.
      • movedPermanently

        public static HttpStatus movedPermanently​(java.lang.String location)
        Creates an instance with status code 301 Moved Permanently.
        Parameters:
        location - the value of the Location header.
      • movedPermanently

        public static HttpStatus movedPermanently​(Link link)
        Creates an instance with status code 301 Moved Permanently.
        Parameters:
        link - the value of the Location header.
      • temporaryRedirect

        public static HttpStatus temporaryRedirect​(java.lang.String location)
        Creates an instance with status code 302 Found.
        Parameters:
        location - the value of the Location header.
      • temporaryRedirect

        public static HttpStatus temporaryRedirect​(Link location)
        Creates an instance with status code 302 Found.
        Parameters:
        location - the value of the Location header.
      • withLocation

        public HttpStatus withLocation​(java.lang.String location)
        Sets a redirect by using the Location HTTP header.
      • withHttpHeader

        public HttpStatus withHttpHeader​(java.lang.String name,
                                         java.lang.String value)
        Sets an HTTP header. If an existing value for this header already exists, it gets overwritten. If you need to set multiple headers or add them without overwriting existing ones, you need to implement StreamResponse instead.
      • getStatusCode

        public int getStatusCode()
        Returns the status code.
      • getResponseBody

        public java.lang.String getResponseBody()
        Returns the response body.
      • getContentType

        public java.lang.String getContentType()
        Returns the MIME content type of the response body.
      • getExtraHttpHeaders

        public java.util.Map<java.lang.String,​java.lang.String> getExtraHttpHeaders()
        Returns the extra HTTP headers.