Class BeanValidationInterceptor<M extends Message>
java.lang.Object
org.axonframework.messaging.core.interception.BeanValidationInterceptor<M>
- Type Parameters:
M- The message type this interceptor can process.
- All Implemented Interfaces:
MessageDispatchInterceptor<M>,MessageHandlerInterceptor<M>
public class BeanValidationInterceptor<M extends Message>
extends Object
implements MessageHandlerInterceptor<M>, MessageDispatchInterceptor<M>
Interceptor that applies JSR303 bean validation on incoming
Messages. When validation on a message fails, a
JSR303ViolationException is thrown, holding the constraint violations. This interceptor can either be used as
a MessageHandlerInterceptor or as a MessageDispatchInterceptor.- Since:
- 1.1.0
- Author:
- Allard Buijze
-
Constructor Summary
ConstructorsConstructorDescriptionInitializes a validation interceptor using a defaultValidatorFactory.BeanValidationInterceptor(jakarta.validation.ValidatorFactory validatorFactory) Initializes a validation interceptor using the givenValidatorFactory. -
Method Summary
Modifier and TypeMethodDescriptioninterceptOnDispatch(M message, ProcessingContext context, MessageDispatchInterceptorChain<M> dispatchInterceptorChain) Intercepts a givenmessageon dispatching.interceptOnHandle(M message, ProcessingContext context, MessageHandlerInterceptorChain<M> handlerInterceptorChain) Intercepts a givenmessageon handling before reaching the designatedMessageHandler.validateMessage(Object message, jakarta.validation.Validator validator) Validate the givenmessageusing the givenvalidator.
-
Constructor Details
-
BeanValidationInterceptor
public BeanValidationInterceptor()Initializes a validation interceptor using a defaultValidatorFactory.- See Also:
-
BeanValidationInterceptor
public BeanValidationInterceptor(jakarta.validation.ValidatorFactory validatorFactory) Initializes a validation interceptor using the givenValidatorFactory.- Parameters:
validatorFactory- the factory providingValidatorinstances for this interceptor
-
-
Method Details
-
interceptOnDispatch
@Nonnull public MessageStream<?> interceptOnDispatch(@Nonnull M message, @Nullable ProcessingContext context, @Nonnull MessageDispatchInterceptorChain<M> dispatchInterceptorChain) Description copied from interface:MessageDispatchInterceptorIntercepts a givenmessageon dispatching. The implementer of this method might want to intercept the message before passing it to the chain (effectively before callingMessageDispatchInterceptorChain.proceed(Message, ProcessingContext)) or after the chain (by mapping the resulting message by callingMessageStream.mapMessage(Function)).- Specified by:
interceptOnDispatchin interfaceMessageDispatchInterceptor<M extends Message>- Parameters:
message- The message to intercept on dispatching.context- The active processing context, if any. Can be used to (e.g.) validate correlation data.dispatchInterceptorChain- The interceptor chain to signal that processing is finished and further interceptors should be called.- Returns:
- The resulting message stream from
MessageDispatchInterceptorChain.proceed(Message, ProcessingContext).
-
interceptOnHandle
@Nonnull public MessageStream<?> interceptOnHandle(@Nonnull M message, @Nonnull ProcessingContext context, @Nonnull MessageHandlerInterceptorChain<M> handlerInterceptorChain) Description copied from interface:MessageHandlerInterceptorIntercepts a givenmessageon handling before reaching the designatedMessageHandler. The interceptor is responsible for the continuation of the handling process by invoking theMessageHandlerInterceptorChain.proceed(Message, ProcessingContext)method on the giveninterceptorChain. The givencontextcontains contextual information. Any information gathered by interceptors may be attached to the context. Interceptors are not allowed to change the type of the message handling result, as the dispatching component expects a result of a specific type.- Specified by:
interceptOnHandlein interfaceMessageHandlerInterceptor<M extends Message>- Parameters:
message- The message to intercept on handling.context- The active processing context of themessagebeing processed.handlerInterceptorChain- The interceptor chain that allows this interceptor to proceed the dispatch process.- Returns:
- The resulting message stream from
MessageHandlerInterceptorChain.proceed(Message, ProcessingContext).
-
validateMessage
protected Set<jakarta.validation.ConstraintViolation<Object>> validateMessage(Object message, jakarta.validation.Validator validator) Validate the givenmessageusing the givenvalidator. The default implementation merely callsvalidator.validate(message).Subclasses may override this method to alter the validation behavior in specific cases. Although the
nullis accepted as return value to indicate that there are no constraint violations, implementations are encouraged to return an empty Set instead.- Parameters:
message- the message to validatevalidator- the validator provided by the validator factory- Returns:
- a set of
ConstraintViolations. May also returnnull
-