5

How to perform validation with Spring Cloud Stream framework in message listeners using standard Spring annotation based validation?

Tried different cases, with @Valid @Payloadfor incoming object, tried method validation post processor with @Validated on entity, but it didn't help.

@StreamListener(MediaItemStream.ITEM_LIKED_CHANNEL)
public void handleLikeMessage(@Valid @Payload LikeInputDto like) {...

and

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}

The best approach for now is just using of custom service for validation, but it looks not as wanted..

@Log4j2
@Service
@AllArgsConstructor
public class LikeStreamHandler {

    private MediaEventMessagingService mediaEventMessagingService;
    private ValidationService validationService;

    @StreamListener(MediaItemStream.ITEM_LIKED_CHANNEL)
    public void handleLikeMessage(LikeInputDto like) {
        validationService.validate(like);

        log.debug("Handling LIKE message: {}", like);
        mediaEventMessagingService.processLikeEvent(like);
    }
}
Oleksandr Yefymov
  • 6,081
  • 2
  • 22
  • 32

1 Answers1

3

This is a new Feature of Spring Cloud Stream v2.1.0: Issue on GitHub: "Add (javax.)Validation Support for Stream Listener"

Josef Reichardt
  • 2,778
  • 3
  • 22
  • 37