Class RestApiResponseBuilder


  • public class RestApiResponseBuilder
    extends java.lang.Object
    Build a RestApiResponse specifying response body, status and other HTTP attributes.
    • Field Detail

      • response

        protected java.io.Serializable response
      • httpStatus

        protected int httpStatus
      • additionalHeaders

        protected final java.util.Map<java.lang.String,​java.lang.String> additionalHeaders
      • additionalCookies

        protected final java.util.List<javax.servlet.http.Cookie> additionalCookies
      • characterSet

        protected java.lang.String characterSet
      • mediaType

        protected java.lang.String mediaType
    • Constructor Detail

      • RestApiResponseBuilder

        public RestApiResponseBuilder()
    • Method Detail

      • withResponse

        public RestApiResponseBuilder withResponse​(java.io.Serializable response)
        Set the body of the response
        Parameters:
        response - the response body
      • withResponseStatus

        public RestApiResponseBuilder withResponseStatus​(int httpStatus)
        Set the HTTP status of the response. By default, OK status (200) is set.
        Parameters:
        httpStatus - the HTTP status of the response
        See Also:
        HttpServletResponse
      • withAdditionalHeader

        public RestApiResponseBuilder withAdditionalHeader​(java.lang.String name,
                                                           java.lang.String value)
        Adds a header in the HTTP response
        Parameters:
        name - the name of the header to add in the response.
        value - the value for this header
        See Also:
        HttpHeaders
      • withAdditionalCookie

        public RestApiResponseBuilder withAdditionalCookie​(javax.servlet.http.Cookie cookie)
        Adds a cookie to the HTTP response
        Parameters:
        cookie - the Cookie to add to the response
      • withCharacterSet

        public RestApiResponseBuilder withCharacterSet​(java.lang.String characterSet)
        Set the character set of the HTTP response. By default UTF-8 is set.
        Parameters:
        characterSet - the name of the character set
        See Also:
        Charset
      • withMediaType

        public RestApiResponseBuilder withMediaType​(java.lang.String mediaType)
        Set the media type of the HTTP response body. By default "application/json" is set.
        Parameters:
        mediaType - the media type to set.
        See Also:
        Registered media types
      • withContentRange

        public RestApiResponseBuilder withContentRange​(int pageIndex,
                                                       int pageSize)
        When returning a paged result, sets the start index and the page size. Setting content range overrides the Content-Range header of the response.
        Parameters:
        pageIndex - the start index of the returned page.
        pageSize - the size of the returned page.
        Returns:
        the RestApiResponseBuilder
      • withContentRange

        public RestApiResponseBuilder withContentRange​(int pageIndex,
                                                       int pageSize,
                                                       long totalSize)
        When returning a paged result, sets the start index, the page size and the total size. Setting content range overrides the Content-Range header of the response.
        Parameters:
        pageIndex - the start index of the returned page.
        pageSize - the size of the returned page.
        totalSize - the total size of the requested entity.
        Returns:
        the RestApiResponseBuilder
      • build

        public RestApiResponse build()
        Returns:
        the RestApiResponse response