Interface ResponseType<R>
- Type Parameters:
R- the generic type of thisResponseTypeto be matched and converted.
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
AbstractResponseType,InstanceResponseType,MultipleInstancesResponseType,OptionalResponseType,PublisherResponseType
Specifies the expected response type required when performing a query through the
QueryBus/QueryGateway. By wrapping the
response type as a generic R, we can easily service the expected response as a single instance, a list, a
page etc., based on the selected implementation even while the query handler return type might be slightly
different.
It is in charge of matching the response type of a query handler with the given generic R. If this match
returns true, it signals the found query handler can handle the intended query. As a follow up, the response
retrieved from a query handler should move through the convert(Object) function to guarantee the
right response type is returned.- Since:
- 3.2
- Author:
- Steven van Beelen
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault Rdefault ResponseType<?> Returns theResponseTypeinstance that should be used when serializing responses.Class<?> Gets actual response type or generic placeholder.booleanMatch the query handler its responseTypewith theResponseTypeimplementation its expected response typeR.default IntegerDefines the match and its priority.Returns aClassrepresenting the type of the payload to be contained in the response message.
-
Field Details
-
Method Details
-
matches
Match the query handler its responseTypewith theResponseTypeimplementation its expected response typeR. Will return true if a response can be converted based on the givenresponseTypeand false if it cannot.- Parameters:
responseType- the responseTypeof the query handler which is matched against- Returns:
- true if a response can be converted based on the given
responseTypeand false if it cannot
-
matchRank
Defines the match and its priority. The greater the value above 0, the better the match. This is particularly useful forMultipleInstancesResponseTypeswhen there are matches on a both multiple and single instance types. Lists should be given priority for handling. -
convert
Converts the givenresponseof typeObjectinto the typeRof thisResponseTypeinstance. Should only be called ifmatches(Type)returns true. It is unspecified what this function does if thematches(Type)returned false.- Parameters:
response- theObjectto convert intoR- Returns:
- a
responseof typeR
-
convertExceptional
Converts the givenThrowableinto the typeRof thisResponseTypeinstance. Used when an error is represented as the message payload. TheOptionalis not empty when an error is represented as the message payload.- Returns:
- a
Optionalresponseof typeR
-
responseMessagePayloadType
Returns aClassrepresenting the type of the payload to be contained in the response message.- Returns:
- a
Classrepresenting the type of the payload to be contained in the response message
-
getExpectedResponseType
Class<?> getExpectedResponseType()Gets actual response type or generic placeholder.- Returns:
- actual response type or generic placeholder
-
forSerialization
Returns theResponseTypeinstance that should be used when serializing responses. This method has a default implementation that returnsthis. Implementations that describe a Response Type that is not suited for serialization, should return an alternative that is suitable, and ensure theconvert(Object)is capable of converting that type of response to the request type in this instance.- Returns:
- a
ResponseTypeinstance describing a type suitable for serialization
-