Class ServiceResponse<T>

java.lang.Object
com.morpheusdata.response.ServiceResponse<T>
Type Parameters:
T - The class that holds the data - must be serializable. Usually a Map or List.

public class ServiceResponse<T> extends Object
ServiceResponse is a generic that allows you to strongly type models. Some scenarios are: Respond with text/html of content. Respond with data that will be serialized as json. Respond with data that will be used in a template model. Response headers and cookies can also be set if required.
  • Field Details

    • inProgress

      public Boolean inProgress
  • Constructor Details

    • ServiceResponse

      public ServiceResponse()
    • ServiceResponse

      public ServiceResponse(Boolean success, String msg, Map<String,String> errors, T data)
  • Method Details

    • prepare

      public static ServiceResponse prepare()
      Helper to initialize a base response.
      Returns:
      A generic respose assuming the repsonse is an error if not converted to a success response.
    • prepare

      public static ServiceResponse prepare(Object data)
      Helper to initialize a base response with initial data.
      Returns:
      A generic respose assuming the repsonse is an error if not converted to a success response.
    • create

      public static ServiceResponse create(Map<String,Object> config)
      Helper to build an error response from a generic map.
      Returns:
      A success or error response based on the boolean value of success in the map.
    • create

      public static ServiceResponse create(ServiceResponse source)
      Helper to create service response from an existing service response. Primarly a convenience method to prevent errors when a map has already been converted to a service response.
      Returns:
      An unmodified service response.
    • error

      public static ServiceResponse error()
      Helper to return a generic error response.
      Returns:
      A generic error scenario.
    • error

      public static ServiceResponse error(String msg)
      Helper to return a error message
      Parameters:
      msg - Message to send to the user.
      Returns:
      a ServiceResponse
    • error

      public static ServiceResponse error(String msg, Map<String,String> errors)
      Detailed error message with a list of errors.
      Parameters:
      msg - Message to send to the user.
      errors - Detailed list of errors
      Returns:
      a ServiceResponse
    • error

      public static ServiceResponse error(String msg, Map<String,String> errors, Object data)
      Detailed error message with a list of errors.
      Parameters:
      msg - Message to send to the user.
      errors - Detailed list of errors
      data - Any additional data needed for the view.
      Returns:
      a ServiceResponse
    • success

      public static ServiceResponse success(Object data)
      Helper to return a success message.
      Parameters:
      data - object to pass back in success
      Returns:
      a ServiceResponse
    • success

      public static ServiceResponse success()
      Create a generic success response
      Returns:
      success response
    • toMap

      public Map<String,Object> toMap()
      Build a Map from this object with keys success, msg, errors, data
      Returns:
      response Map
    • toMap

      public Map<String,Object> toMap(String dataKeyName)
      Serializes the ServiceResponse to a map.
      Parameters:
      dataKeyName - the name to assign the data keys key in the map
      Returns:
      A Map
    • hasError

      public boolean hasError(String key)
      Return if the ServiceResponse has any errors set
      Parameters:
      key - Check a specific key
      Returns:
      boolean
    • hasErrors

      public boolean hasErrors()
      Return if the ServiceResponse has any errors set
      Returns:
      boolean
    • toString

      public String toString()
      String representation of the toMap() method
      Overrides:
      toString in class Object
      Returns:
      the response as a String
    • getSuccess

      public Boolean getSuccess()
    • setSuccess

      public void setSuccess(Boolean success)
    • getMsg

      public String getMsg()
    • setMsg

      public void setMsg(String msg)
    • getData

      public T getData()
    • setData

      public void setData(T data)
    • getErrors

      public Map<String,String> getErrors()
    • setErrors

      public void setErrors(Map<String,String> errors)
    • addError

      public void addError(String value)
    • addError

      public void addError(String key, String value)
    • removeError

      public void removeError()
    • clearErrors

      public void clearErrors()
    • removeError

      public void removeError(String key)
    • getError

      public String getError(String key)
      Returns the specific error message for a given key.
      Parameters:
      key - that contains the error
      Returns:
      The error value
    • getError

      public String getError()
      Provided for backwards compatibility with existing getError()
      Returns:
      Error message
    • setError

      public void setError(String value)
      Provided for backwards compatibility with existing setError(msg)
      Parameters:
      value - value to set
    • getHeaders

      public Map<String,Object> getHeaders()
    • setHeaders

      public void setHeaders(Map<String,Object> headers)
    • addHeader

      public void addHeader(String key, Object value)
    • getContent

      public String getContent()
    • setContent

      public void setContent(String content)
    • getErrorCode

      public String getErrorCode()
    • setErrorCode

      public void setErrorCode(String errorCode)
    • getStatusCode

      public String getStatusCode()
    • setStatusCode

      public void setStatusCode(String statusCode)
    • getResults

      public Object getResults()
    • setResults

      public void setResults(Object results)
    • getCookies

      public Map getCookies()
    • setCookies

      public void setCookies(Map cookies)
    • addCookie

      public void addCookie(String key, Object value)
      Add a Cookie to the response
      Parameters:
      key - cookie name
      value - cookie value
    • getCookie

      public String getCookie(String key)
      Find a cookie
      Parameters:
      key - cookie name
      Returns:
      the cookie value