Add validation support into WebFlux Inbound (#2977)

* Add validation support into WebFlux Inbound

See https://stackoverflow.com/questions/56729517/spring-integration-webflux-validate-input-json-body

* Introduce a `WebFluxInboundEndpoint.validator` option to validate
a request payload after conversion HTTP request into a `Publisher`
* Expose a `WebFluxInboundEndpointSpec.validator()` option
* Introduce a `IntegrationWebExchangeBindException` to extend a
`WebExchangeBindException` for Spring Integration use-case.
such an exception is used in the `ResponseStatusExceptionHandler`
extensions, like one in Spring Boot for proper response rendering
* Test and document the solution

* * Remove unused imports from IntegrationWebExchangeBindException
This commit is contained in:
Artem Bilan
2019-06-26 11:11:33 -04:00
committed by Gary Russell
parent 97aaf95ccb
commit 59cdc4ddae
7 changed files with 231 additions and 46 deletions

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.http.support;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.support.WebExchangeBindException;
/**
* A {@link WebExchangeBindException} extension for validation error with a failed
* message context.
* We can't rely on the default {@link WebExchangeBindException} behavior since
* there is no POJO method invocation in Spring Integration Web endpoint implementations.
*
* @author Artem Bilan
*
* @since 5.2
*/
@SuppressWarnings("serial")
public class IntegrationWebExchangeBindException extends WebExchangeBindException {
private final String endpointId;
private final Object failedPayload;
public IntegrationWebExchangeBindException(String endpointId, Object failedPayload,
BindingResult bindingResult) {
super(null, bindingResult); // NOSONAR - we ignore a MethodParameter in favor of payload and endpoint context
this.endpointId = endpointId;
this.failedPayload = failedPayload;
}
@Override
public String getMessage() {
BindingResult bindingResult = getBindingResult();
StringBuilder sb = new StringBuilder("Validation failed for payload ")
.append(this.failedPayload)
.append(" in the endpoint ")
.append(this.endpointId)
.append(", with ")
.append(bindingResult.getErrorCount())
.append(" error(s): ");
for (ObjectError error : bindingResult.getAllErrors()) {
sb.append("[").append(error).append("] ");
}
return sb.toString();
}
}