001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.reactive.result.method.annotation; 018 019import reactor.core.publisher.Mono; 020 021import org.springframework.core.Conventions; 022import org.springframework.core.MethodParameter; 023import org.springframework.core.ReactiveAdapter; 024import org.springframework.core.ReactiveAdapterRegistry; 025import org.springframework.util.Assert; 026import org.springframework.util.StringUtils; 027import org.springframework.validation.BindingResult; 028import org.springframework.validation.Errors; 029import org.springframework.web.bind.annotation.ModelAttribute; 030import org.springframework.web.reactive.BindingContext; 031import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport; 032import org.springframework.web.server.ServerWebExchange; 033 034/** 035 * Resolve {@link Errors} or {@link BindingResult} method arguments. 036 * An {@code Errors} argument is expected to appear immediately after the 037 * model attribute in the method signature. 038 * 039 * @author Rossen Stoyanchev 040 * @since 5.0 041 */ 042public class ErrorsMethodArgumentResolver extends HandlerMethodArgumentResolverSupport { 043 044 public ErrorsMethodArgumentResolver(ReactiveAdapterRegistry registry) { 045 super(registry); 046 } 047 048 049 @Override 050 public boolean supportsParameter(MethodParameter parameter) { 051 return checkParameterType(parameter, Errors.class::isAssignableFrom); 052 } 053 054 @Override 055 public Mono<Object> resolveArgument( 056 MethodParameter parameter, BindingContext context, ServerWebExchange exchange) { 057 058 Object errors = getErrors(parameter, context); 059 if (Mono.class.isAssignableFrom(errors.getClass())) { 060 return ((Mono<?>) errors).cast(Object.class); 061 } 062 else if (Errors.class.isAssignableFrom(errors.getClass())) { 063 return Mono.just(errors); 064 } 065 else { 066 throw new IllegalStateException("Unexpected Errors/BindingResult type: " + errors.getClass().getName()); 067 } 068 } 069 070 private Object getErrors(MethodParameter parameter, BindingContext context) { 071 Assert.isTrue(parameter.getParameterIndex() > 0, 072 "Errors argument must be declared immediately after a model attribute argument"); 073 074 int index = parameter.getParameterIndex() - 1; 075 MethodParameter attributeParam = MethodParameter.forExecutable(parameter.getExecutable(), index); 076 ReactiveAdapter adapter = getAdapterRegistry().getAdapter(attributeParam.getParameterType()); 077 078 Assert.state(adapter == null, "An @ModelAttribute and an Errors/BindingResult argument " + 079 "cannot both be declared with an async type wrapper. " + 080 "Either declare the @ModelAttribute without an async wrapper type or " + 081 "handle a WebExchangeBindException error signal through the async type."); 082 083 ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); 084 String name = (ann != null && StringUtils.hasText(ann.value()) ? 085 ann.value() : Conventions.getVariableNameForParameter(attributeParam)); 086 Object errors = context.getModel().asMap().get(BindingResult.MODEL_KEY_PREFIX + name); 087 088 Assert.state(errors != null, () -> "An Errors/BindingResult argument is expected " + 089 "immediately after the @ModelAttribute argument to which it applies. " + 090 "For @RequestBody and @RequestPart arguments, please declare them with a reactive " + 091 "type wrapper and use its onError operators to handle WebExchangeBindException: " + 092 parameter.getMethod()); 093 094 return errors; 095 } 096 097}