spring-cloud-stream icon indicating copy to clipboard operation
spring-cloud-stream copied to clipboard

Add (javax.)Validation Support for functional interface

Open GreenRover opened this issue 4 years ago • 3 comments

For Stream binder there for support for the javax validation: https://github.com/spring-cloud/spring-cloud-stream/commit/d93f170f2e82319300b825c0437523353fc3c590

But it seams for the functional interface it is gone. It would expect this throwing a error when a message with invalid person name was received instead calling the log() method.

@SpringBootApplication
public class LoggingConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoggingConsumerApplication.class, args);
    }

    @Bean
    public Consumer<Message<@Valid Person>> log() {
        return msg -> {
            System.out.println("Received: " + msg.getPayload());
        };
    }

    public static class Person {
        private String name;
        public String getName() {
            return name;
        }
        @Pattern(regexp = "^[a-zA-Z_:][a-zA-Z0-9_:]*$")
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            return this.name;
        }
    }
}

GreenRover avatar Jun 03 '21 13:06 GreenRover

I don't think that can work. You can't access the Annoations of a consumer according to my understanding (https://stackoverflow.com/questions/25702618/java-8-get-consumer-underlying-function-annotation). Spring CloudStream only can access the adhoc Implementation of the consumer which hides the real implementation.

AnWeber avatar Feb 24 '22 13:02 AnWeber

Shure it can, it use very advanced reflection usage.

GreenRover avatar Feb 24 '22 18:02 GreenRover

@GreenRover Do you have a tip for me on how to search? I have looked at the objects during debugging and have not found a starting point.

AnWeber avatar Feb 24 '22 19:02 AnWeber