Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

spring - @Valid @Payload on @KafkaHandler – bug or missing feature?

We use Spring Kafka together with Spring Boot (all latest versions). We switched handling of Kafka messages into @KafkaHandler annotated methods and expected that @Valid/@Validated together with @Payload will ensure payload validation, but that did not happen. This feature is working for @KafkaListener, should it be also working for @KafkaHandler?

@KafkaListener(...)
@Component
public class NotificationListener {

    @KafkaHandler
    public void handleV1(@Payload @Valid NotificationV1 notification) {

Thank you.

question from:https://stackoverflow.com/questions/65645415/valid-payload-on-kafkahandler-bug-or-missing-feature

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Validator is not applied in this case because we just don't reach the PayloadMethodArgumentResolver for that purpose.

The target payload for the multi-method @KafkaListener is resolved before we call the method because we definitely need to know which method to call. Such a logic is done in the InvocableHandlerMethod.getMethodArgumentValues():

        args[i] = findProvidedArgument(parameter, providedArgs);
        if (args[i] != null) {
            continue;
        }
        ...
        try {
            args[i] = this.resolvers.resolveArgument(parameter, message);
        }

The Validator functionality is done in those resolvers. The findProvidedArgument() gives us the payload converted before for execution and here we just don't check any annotations on parameters.

We probably need to poll validation logic into the DelegatingInvocableHandler when we have selected a handler and before its invocation...

Feel free to raise a GitHub issue so we don't forget that this is needed to be addressed somehow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...