Search in sources :

Example 1 with WebExchangeBindException

use of org.springframework.web.bind.support.WebExchangeBindException in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
    ResolvableType type = ResolvableType.forMethodParameter(parameter);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
    ResolvableType valueType = (adapter != null ? type.getGeneric(0) : type);
    Assert.state(adapter == null || !adapter.isMultiValue(), getClass().getSimpleName() + " doesn't support multi-value reactive type wrapper: " + parameter.getGenericParameterType());
    String name = getAttributeName(valueType, parameter);
    Mono<?> valueMono = getAttributeMono(name, valueType, context.getModel());
    Map<String, Object> model = context.getModel().asMap();
    MonoProcessor<BindingResult> bindingResultMono = MonoProcessor.create();
    model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResultMono);
    return valueMono.then(value -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
        return binder.bind(exchange).doOnError(bindingResultMono::onError).doOnSuccess(aVoid -> {
            validateIfApplicable(binder, parameter);
            BindingResult errors = binder.getBindingResult();
            model.put(BindingResult.MODEL_KEY_PREFIX + name, errors);
            model.put(name, value);
            bindingResultMono.onNext(errors);
        }).then(Mono.fromCallable(() -> {
            BindingResult errors = binder.getBindingResult();
            if (adapter != null) {
                return adapter.fromPublisher(errors.hasErrors() ? Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono);
            } else {
                if (errors.hasErrors() && !hasErrorsArgument(parameter)) {
                    throw new WebExchangeBindException(parameter, errors);
                }
                return value;
            }
        }));
    });
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) Errors(org.springframework.validation.Errors) Validated(org.springframework.validation.annotation.Validated) ClassUtils(org.springframework.util.ClassUtils) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) MonoProcessor(reactor.core.publisher.MonoProcessor) Mono(reactor.core.publisher.Mono) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethodArgumentResolverSupport(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) Annotation(java.lang.annotation.Annotation) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) BindingResult(org.springframework.validation.BindingResult) ResolvableType(org.springframework.core.ResolvableType) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 2 with WebExchangeBindException

use of org.springframework.web.bind.support.WebExchangeBindException in project spring-framework by spring-projects.

the class AbstractMessageReaderArgumentResolver method validate.

private void validate(Object target, Object[] validationHints, MethodParameter param, BindingContext binding, ServerWebExchange exchange) {
    String name = Conventions.getVariableNameForParameter(param);
    WebExchangeDataBinder binder = binding.createDataBinder(exchange, target, name);
    binder.validate(validationHints);
    if (binder.getBindingResult().hasErrors()) {
        throw new WebExchangeBindException(param, binder.getBindingResult());
    }
}
Also used : WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException)

Example 3 with WebExchangeBindException

use of org.springframework.web.bind.support.WebExchangeBindException in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method testValidationError.

private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor) throws URISyntaxException {
    ServerWebExchange exchange = postForm("age=invalid");
    Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
    mono = valueMonoExtractor.apply(mono);
    StepVerifier.create(mono).consumeErrorWith(ex -> {
        assertTrue(ex instanceof WebExchangeBindException);
        WebExchangeBindException bindException = (WebExchangeBindException) ex;
        assertEquals(1, bindException.getErrorCount());
        assertTrue(bindException.hasFieldErrors("age"));
    }).verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) Single(rx.Single) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Duration(java.time.Duration) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Before(org.junit.Before) Validated(org.springframework.validation.annotation.Validated) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ResolvableMethod(org.springframework.web.method.ResolvableMethod) RxReactiveStreams(rx.RxReactiveStreams) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException)

Aggregations

WebExchangeBindException (org.springframework.web.bind.support.WebExchangeBindException)3 Map (java.util.Map)2 MethodParameter (org.springframework.core.MethodParameter)2 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)2 BindingResult (org.springframework.validation.BindingResult)2 Validated (org.springframework.validation.annotation.Validated)2 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)2 WebExchangeDataBinder (org.springframework.web.bind.support.WebExchangeDataBinder)2 BindingContext (org.springframework.web.reactive.BindingContext)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 Mono (reactor.core.publisher.Mono)2 Annotation (java.lang.annotation.Annotation)1 URISyntaxException (java.net.URISyntaxException)1 Duration (java.time.Duration)1 Function (java.util.function.Function)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertSame (org.junit.Assert.assertSame)1 Assert.assertTrue (org.junit.Assert.assertTrue)1