使用 JSR303 进行入站 http 消息验证

Inbound http message validation with JSR303

我正在使用 Spring 集成来接收 http 消息,然后将其放入频道并进行一些转换。

我阅读了文档 (https://docs.spring.io/spring-integration/reference/html/http.html),它看起来像:

@Bean
public HttpRequestHandlingMessagingGateway inbound() {
    HttpRequestHandlingMessagingGateway gateway =
        new HttpRequestHandlingMessagingGateway(true);
    gateway.setRequestMapping(mapping());
    gateway.setRequestPayloadType(SomeBean.class);
    gateway.setRequestChannelName("httpRequest");
    return gateway;
}

我想使用 JSR 303 bean 验证 (https://beanvalidation.org/1.0/spec/) 来验证负载,是否可行?什么是最好的方法?

提前致谢!

关于验证有一个专门的段落:https://docs.spring.io/spring-integration/reference/html/http.html#http-validation。因此,您只需要使用该网关的 setValidator()

/**

 * Specify a {@link Validator} to validate a converted payload from request.

 * @param validator the {@link Validator} to use.

 * @since 5.2

 */

public void setValidator(Validator validator) {

    this.validator = validator;

}

验证API来自Spring框架:https://docs.spring.io/spring/docs/5.2.4.RELEASE/spring-framework-reference/core.html#validation