简体   繁体   中英

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 @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);
    }
}

这是Spring Cloud Stream v2.1.0的新功能: GitHub上的问题:“为流监听器添加(javax。)验证支持”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM