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 Details

    • BeanValidationInterceptor

      public BeanValidationInterceptor()
      Initializes a validation interceptor using a default ValidatorFactory.
      See Also:
      • Validation.buildDefaultValidatorFactory()
    • BeanValidationInterceptor

      public BeanValidationInterceptor(jakarta.validation.ValidatorFactory validatorFactory)
      Initializes a validation interceptor using the given ValidatorFactory.
      Parameters:
      validatorFactory - the factory providing Validator instances for this interceptor
  • Method Details

    • interceptOnDispatch

      @Nonnull public MessageStream<?> interceptOnDispatch(@Nonnull M message, @Nullable ProcessingContext context, @Nonnull MessageDispatchInterceptorChain<M> dispatchInterceptorChain)
      Description copied from interface: MessageDispatchInterceptor
      Intercepts a given message on dispatching.

      The implementer of this method might want to intercept the message before passing it to the chain (effectively before calling MessageDispatchInterceptorChain.proceed(Message, ProcessingContext)) or after the chain (by mapping the resulting message by calling MessageStream.mapMessage(Function)).

      Specified by:
      interceptOnDispatch in interface MessageDispatchInterceptor<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: MessageHandlerInterceptor
      Intercepts a given message on handling before reaching the designated MessageHandler.

      The interceptor is responsible for the continuation of the handling process by invoking the MessageHandlerInterceptorChain.proceed(Message, ProcessingContext) method on the given interceptorChain.

      The given context contains 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:
      interceptOnHandle in interface MessageHandlerInterceptor<M extends Message>
      Parameters:
      message - The message to intercept on handling.
      context - The active processing context of the message being 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 given message using the given validator. The default implementation merely calls validator.validate(message).

      Subclasses may override this method to alter the validation behavior in specific cases. Although the null is 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 validate
      validator - the validator provided by the validator factory
      Returns:
      a set of ConstraintViolations. May also return null