0

I'm using Spring Integration to receive an http message and then put it in a channel and do some transformations.

I read the documentation (https://docs.spring.io/spring-integration/reference/html/http.html) and it will look like:

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

I want to validate the payload using JSR 303 bean validation (https://beanvalidation.org/1.0/spec/), is it possible? What is the best way?

Thanks in advance!

italktothewind
  • 1,950
  • 2
  • 28
  • 55

1 Answers1

0

There is a dedicated paragraph about validation: https://docs.spring.io/spring-integration/reference/html/http.html#http-validation. So, you just need to use a setValidator() of that gateway:

/**

 * 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;

}

The validation API comes from Spring Framework: https://docs.spring.io/spring/docs/5.2.4.RELEASE/spring-framework-reference/core.html#validation

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • I read it! But that is a Spring Validator, not a JSR303 validator. – italktothewind Mar 11 '20 at 16:53
  • Looks like you didn't too far: https://docs.spring.io/spring/docs/5.2.4.RELEASE/spring-framework-reference/core.html#validation-beanvalidation. See that `LocalValidatorFactoryBean` and its JavaDocs: ` * This is the central class for {@code javax.validation} (JSR-303) setup in a Spring`. Not sure why you think we talk about different approaches... Spring just provides its own abstraction where one of the implementation is really for that JSR-303 – Artem Bilan Mar 11 '20 at 16:59