Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Stream validation

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);
    }
}
like image 821
Oleksandr Yefymov Avatar asked Oct 17 '22 02:10

Oleksandr Yefymov


1 Answers

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

like image 187
Josef Reichardt Avatar answered Nov 03 '22 23:11

Josef Reichardt