Validation 验证Spring Kafka有效载荷

Validation 验证Spring Kafka有效载荷,validation,spring-boot,bean-validation,spring-kafka,Validation,Spring Boot,Bean Validation,Spring Kafka,我正在尝试设置一个同时具有REST(POST)端点和Kafka端点的服务,这两个端点都应该采用请求对象的JSON表示(我们称之为Foo)。我希望确保Foo对象是有效的(通过JSR-303或其他方式)。所以Foo可能看起来像: public class Foo { @Max(10) private int bar; // Getter and setter boilerplate } 设置REST端点很容易: @PostMapping(value = "/", prod

我正在尝试设置一个同时具有REST(POST)端点和Kafka端点的服务,这两个端点都应该采用请求对象的JSON表示(我们称之为Foo)。我希望确保Foo对象是有效的(通过JSR-303或其他方式)。所以Foo可能看起来像:

public class Foo {
    @Max(10)
    private int bar;

    // Getter and setter boilerplate
}
设置REST端点很容易:

@PostMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> restEndpoint(@Valid @RequestBody Foo foo) {
    // Do stuff here
}
但无论我做什么尝试,我仍然可以传入无效的请求,并且它们不会出错

我尝试了
@Valid
@Validated
。我已经尝试添加一个
MethodValidationPostProcessor
bean。我已尝试向KafkaListenerEndpointRegistrator(启用Kafka javadoc)添加验证程序:


我已经花了几天时间在这上面了,我已经没有其他的想法了。这是否可能(不将验证写入我的每个kakfa端点)?

很抱歉延迟;本周我们在SpringOne平台


基础结构当前未将
验证程序
传递到有效负载参数解析程序。请打开一个。

下面是有关此讨论的更多内容,
@KafkaListener(topics = "fooTopic")
public void kafkaEndpoint(@Valid @Payload Foo foo) {
    // I shouldn't get here with an invalid object!!!
    logger.debug("Successfully processed the object" + foo);

    // But just to make sure, let's see if hand-validating it works
    Validator validator = localValidatorFactoryBean.getValidator();
    Set<ConstraintViolation<SlackMessage>> errors = validator.validate(foo);
    if (errors.size() > 0) {
        logger.debug("But there were validation errors!" + errors);
    }
}
@Configuration
public class MiscellaneousConfiguration implements KafkaListenerConfigurer {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    LocalValidatorFactoryBean validatorFactory;

    @Override
    public void configureKafkaListeners(KafkaListenerEndpointRegistrar registrar) {
        logger.debug("Configuring " + registrar);
        registrar.setMessageHandlerMethodFactory(kafkaHandlerMethodFactory());

    }

    @Bean
    public MessageHandlerMethodFactory kafkaHandlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setValidator(validatorFactory);
        return factory;
    }
}