How to perform validation with Spring Cloud Stream framework in message listeners using standard Spring annotation based validation?
Tried different cases, with @Valid @Payload
for 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);
}
}
This is a new Feature of Spring Cloud Stream v2.1.0: Issue on GitHub: "Add (javax.)Validation Support for Stream Listener"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With