Package com.morpheusdata.response
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.
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 Summary
-
Constructor Summary
ConstructorDescription -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a Cookie to the responsevoid
void
void
void
static ServiceResponse
create
(ServiceResponse source) Helper to create service response from an existing service response.static ServiceResponse
Helper to build an error response from a generic map.static ServiceResponse
error()
Helper to return a generic error response.static ServiceResponse
Helper to return a error messagestatic ServiceResponse
Detailed error message with a list of errors.static ServiceResponse
Detailed error message with a list of errors.Find a cookiegetData()
getError()
Provided for backwards compatibility with existing getError()Returns the specific error message for a given key.getMsg()
boolean
Return if the ServiceResponse has any errors setboolean
Return if the ServiceResponse has any errors setstatic ServiceResponse
prepare()
Helper to initialize a base response.static ServiceResponse
Helper to initialize a base response with initial data.void
void
removeError
(String key) void
setContent
(String content) void
setCookies
(Map cookies) void
void
Provided for backwards compatibility with existing setError(msg)void
setErrorCode
(String errorCode) void
void
setHeaders
(Map<String, Object> headers) void
void
setResults
(Object results) void
setStatusCode
(String statusCode) void
setSuccess
(Boolean success) static ServiceResponse
success()
Create a generic success responsestatic ServiceResponse
Helper to return a success message.toMap()
Build a Map from this object with keys success, msg, errors, dataSerializes the ServiceResponse to a map.toString()
String representation of the toMap() method
-
Field Details
-
inProgress
-
-
Constructor Details
-
ServiceResponse
public ServiceResponse() -
ServiceResponse
-
-
Method Details
-
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
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
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
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
Helper to return a generic error response.- Returns:
- A generic error scenario.
-
error
Helper to return a error message- Parameters:
msg
- Message to send to the user.- Returns:
- a ServiceResponse
-
error
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
Detailed error message with a list of errors.- Parameters:
msg
- Message to send to the user.errors
- Detailed list of errorsdata
- Any additional data needed for the view.- Returns:
- a ServiceResponse
-
success
Helper to return a success message.- Parameters:
data
- object to pass back in success- Returns:
- a ServiceResponse
-
success
Create a generic success response- Returns:
- success response
-
toMap
Build a Map from this object with keys success, msg, errors, data- Returns:
- response Map
-
toMap
Serializes the ServiceResponse to a map.- Parameters:
dataKeyName
- the name to assign the data keys key in the map- Returns:
- A Map
-
hasError
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
String representation of the toMap() method -
getSuccess
-
setSuccess
-
getMsg
-
setMsg
-
getData
-
setData
-
getErrors
-
setErrors
-
addError
-
addError
-
removeError
public void removeError() -
clearErrors
public void clearErrors() -
removeError
-
getError
Returns the specific error message for a given key.- Parameters:
key
- that contains the error- Returns:
- The error value
-
getError
Provided for backwards compatibility with existing getError()- Returns:
- Error message
-
setError
Provided for backwards compatibility with existing setError(msg)- Parameters:
value
- value to set
-
getHeaders
-
setHeaders
-
addHeader
-
getContent
-
setContent
-
getErrorCode
-
setErrorCode
-
getStatusCode
-
setStatusCode
-
getResults
-
setResults
-
getCookies
-
setCookies
-
addCookie
Add a Cookie to the response- Parameters:
key
- cookie namevalue
- cookie value
-
getCookie
Find a cookie- Parameters:
key
- cookie name- Returns:
- the cookie value
-