Generic value object representing the outcome of an operation - a status flag, human-readable messages, optional field-level validation messages and an arbitrary data payload - intended for serialisation to JSON as the response of a controller action or a GraalJS-exported service method. Provides fluent helper methods for building up messages and data attributes so callers can chain calls when constructing a response.
Properties
| Property | Returns | Description |
|---|---|---|
| data | Object | Arbitrary payload data returned with this result. Expected to be JSON friendly, such as a Map, List or primitive value. May be null if no data has been set. |
| fieldMessages | List<FieldMessage> | Validation or error messages relating to specific fields, where each field name corresponds to a POST variable name. May be null or empty if no field messages have been added. |
| messages | List<String> | General messages relating to this result that are not specific to any one field. May be null or empty if no general messages have been added. |
| nextHref | String | The href to navigate to as a result of this operation. If an object was created this is the href of that object; if the operation completed a step in a workflow this is the href of the next step. May be null. |
| status | boolean | Flag indicating success or failure of the operation this result represents. |
Methods
toMap() · dataAttribute(String key, Object value) · dataAttributes(Map<String,Object> attributes) · fieldMessage(String fieldName, String message) · message(String message) · addMessage(String s) · addFieldMessage(String field, String message) · writeWithNiceDates(OutputStream out) · write(OutputStream out) · isStatus() · setStatus(boolean status) · getNextHref() · setNextHref(String nextHref) · getMessages() · setMessages(List<String> messages) · getFieldMessages() · setFieldMessages(List<FieldMessage> fieldMessages) · getData() · setData(Object data)
toMap()
Returns: Map<String,Object>
Builds a plain, JSON-serialisable map of this result's status, nextHref, messages and field messages. The data property is deliberately left out because it is not known whether it is serialisable.
dataAttribute(String key, Object value)
Returns: JsonResult
Sets a name/value pair on this result's data object, creating the data object as a HashMap first if it is currently null. Throws if data already holds a value that is not a Map.
| Parameter | Description |
|---|---|
key | the attribute name to set on the data map |
value | the value to associate with the key |
dataAttributes(Map<String,Object> attributes)
Returns: JsonResult
Calls dataAttribute for each entry in the given map, converting each value to a string first. Does nothing if attributes is null.
| Parameter | Description |
|---|---|
attributes | the name/value pairs to add to this result's data object |
fieldMessage(String fieldName, String message)
Returns: JsonResult
Same as addFieldMessage but returns this to allow chaining.
| Parameter | Description |
|---|---|
fieldName | the name of the POST variable the message relates to |
message | the validation or error message text |
message(String message)
Returns: JsonResult
Same as addMessage but returns this to allow chaining.
| Parameter | Description |
|---|---|
message | the message text to add |
addMessage(String s)
Returns: void
Adds a message to the messages list, creating the list first if it does not already exist.
| Parameter | Description |
|---|---|
s | the message text to add |
addFieldMessage(String field, String message)
Returns: void
Adds a FieldMessage to the field messages list, creating the list first if it does not already exist.
| Parameter | Description |
|---|---|
field | the name of the POST variable the message relates to |
message | the validation or error message text |
writeWithNiceDates(OutputStream out)
Returns: void
Serialises this result as JSON to the given stream, writing any Date values as a formatted string rather than as a raw long.
| Parameter | Description |
|---|---|
out | the stream to write the JSON output to |
write(OutputStream out)
Returns: void
Serialises this result as JSON to the given stream.
| Parameter | Description |
|---|---|
out | the stream to write the JSON output to |
isStatus()
Returns: boolean
Flag indicating success or failure of the operation this result represents.
setStatus(boolean status)
Returns: void
Sets the success/failure flag for this result.
| Parameter | Description |
|---|---|
status | true if the operation succeeded, false otherwise |
getNextHref()
Returns: String
The href to navigate to as a result of this operation. If an object was created this is the href of that object; if the operation completed a step in a workflow this is the href of the next step. May be null.
setNextHref(String nextHref)
Returns: void
Sets the href to navigate to as a result of this operation.
| Parameter | Description |
|---|---|
nextHref | the next href to navigate to |
getMessages()
Returns: List<String>
General messages relating to this result that are not specific to any one field. May be null or empty if no general messages have been added.
setMessages(List<String> messages)
Returns: void
Replaces the list of general messages on this result.
| Parameter | Description |
|---|---|
messages | the list of general messages to set |
getFieldMessages()
Returns: List<FieldMessage>
Validation or error messages relating to specific fields, where each field name corresponds to a POST variable name. May be null or empty if no field messages have been added.
setFieldMessages(List<FieldMessage> fieldMessages)
Returns: void
Replaces the list of field-specific messages on this result.
| Parameter | Description |
|---|---|
fieldMessages | the list of field messages to set |
getData()
Returns: Object
Arbitrary payload data returned with this result. Expected to be JSON friendly, such as a Map, List or primitive value. May be null if no data has been set.
setData(Object data)
Returns: void
Sets the payload data returned with this result.
| Parameter | Description |
|---|---|
data | the payload data to set |