R
- the generic type of this ResponseType
to be matched and converted.public interface ResponseType<R> extends Serializable
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.to instantiate a {@link ResponseType} implementation
Modifier and Type | Field and Description |
---|---|
static int |
MATCH
Indicates that the response matches with the
Type while returning a single result. |
static int |
NO_MATCH
Indicates that the response type does not match with a given
Type . |
Modifier and Type | Method and Description |
---|---|
default R |
convert(Object response)
|
default Optional<R> |
convertExceptional(Throwable e)
|
default ResponseType<?> |
forSerialization()
Returns the
ResponseType instance that should be used when serializing responses. |
Class<?> |
getExpectedResponseType()
Gets actual response type or generic placeholder.
|
boolean |
matches(Type responseType)
Match the query handler its response
Type with the ResponseType implementation
its expected response type R . |
default Integer |
matchRank(Type responseType)
Defines the match and its priority.
|
Class<R> |
responseMessagePayloadType()
Returns a
Class representing the type of the payload to be contained in the response message. |
static final int NO_MATCH
Type
.static final int MATCH
Type
while returning a single result.boolean matches(Type responseType)
Type
with the ResponseType
implementation
its expected response type R
. Will return true if a response can be converted based on the given responseType
and false if it cannot.responseType
- the response Type
of the query handler which is matched againstresponseType
and false if it cannotdefault Integer matchRank(Type responseType)
MultipleInstancesResponseTypes
when there are matches on a both
multiple and single instance types. Lists should be given priority for handling.
ResponseType#ITERABLE_MATCH
ResponseType#SINGLE_MATCH
NO_MATCH
default R convert(Object response)
response
of type Object
into the type R
of this ResponseType
instance. Should only be called if matches(Type)
returns true. It is
unspecified what this function does if the matches(Type)
returned false.response
- the Object
to convert into R
response
of type R
default Optional<R> convertExceptional(Throwable e)
Throwable
into the type R
of this ResponseType
instance.
Used when an error is represented as the message payload. The Optional
is not empty when an error is
represented as the message payload.Optional
response
of type R
Class<R> responseMessagePayloadType()
Class
representing the type of the payload to be contained in the response message.Class
representing the type of the payload to be contained in the response messageClass<?> getExpectedResponseType()
default ResponseType<?> forSerialization()
ResponseType
instance that should be used when serializing responses. This method has a
default implementation that returns this
. Implementations that describe a Response Type that is not
suited for serialization, should return an alternative that is suitable, and ensure the convert(Object)
is capable of converting that type of response to the request type in this instance.ResponseType
instance describing a type suitable for serializationCopyright © 2010–2023. All rights reserved.