Search code examples
springspring-integrationspring-integration-http

Inbound http message validation with JSR303


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!


Solution

  • 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