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
Fields -
Constructor Summary
ConstructorsConstructorDescription -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a Cookie to the responsevoidvoidvoidvoidstatic ServiceResponsecreate(ServiceResponse source) Helper to create service response from an existing service response.static ServiceResponseHelper to build an error response from a generic map.static ServiceResponseerror()Helper to return a generic error response.static ServiceResponseHelper to return a error messagestatic ServiceResponseDetailed error message with a list of errors.static ServiceResponseDetailed 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()booleanReturn if the ServiceResponse has any errors setbooleanReturn if the ServiceResponse has any errors setstatic ServiceResponseprepare()Helper to initialize a base response.static ServiceResponseHelper to initialize a base response with initial data.voidvoidremoveError(String key) voidsetContent(String content) voidsetCookies(Map cookies) voidvoidProvided for backwards compatibility with existing setError(msg)voidsetErrorCode(String errorCode) voidvoidsetHeaders(Map<String, Object> headers) voidvoidsetResults(Object results) voidsetStatusCode(String statusCode) voidsetSuccess(Boolean success) static ServiceResponsesuccess()Create a generic success responsestatic ServiceResponseHelper 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
-