Spring Cloud Stream验证

Ale*_*mov 5 java validation spring spring-boot spring-cloud-stream

如何使用标准的基于Spring注释的验证在消息侦听器中使用Spring Cloud Stream框架执行验证?

尝试了不同的情况,@Valid @Payload对于传入的对象,尝试使用@Validatedon entity 处理器的方法验证,但它没有帮助.

@StreamListener(MediaItemStream.ITEM_LIKED_CHANNEL)
public void handleLikeMessage(@Valid @Payload LikeInputDto like) {...
Run Code Online (Sandbox Code Playgroud)

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}
Run Code Online (Sandbox Code Playgroud)

目前最好的方法是使用自定义服务进行验证,但它看起来并不像想要的那样.

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)