001/* 002 * Copyright 2002-2019 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 java.util.List; 020 021import reactor.core.publisher.Mono; 022 023import org.springframework.core.MethodParameter; 024import org.springframework.core.ReactiveAdapterRegistry; 025import org.springframework.http.codec.HttpMessageReader; 026import org.springframework.util.Assert; 027import org.springframework.web.bind.annotation.RequestBody; 028import org.springframework.web.reactive.BindingContext; 029import org.springframework.web.server.ServerWebExchange; 030import org.springframework.web.server.ServerWebInputException; 031 032/** 033 * Resolves method arguments annotated with {@code @RequestBody} by reading the 034 * body of the request through a compatible {@code HttpMessageReader}. 035 * 036 * <p>An {@code @RequestBody} method argument is also validated if it is 037 * annotated with {@code @javax.validation.Valid} or 038 * {@link org.springframework.validation.annotation.Validated}. Validation 039 * failure results in an {@link ServerWebInputException}. 040 * 041 * @author Sebastien Deleuze 042 * @author Stephane Maldini 043 * @author Rossen Stoyanchev 044 * @since 5.2 045 */ 046public class RequestBodyMethodArgumentResolver extends AbstractMessageReaderArgumentResolver { 047 048 public RequestBodyMethodArgumentResolver(List<HttpMessageReader<?>> readers, ReactiveAdapterRegistry registry) { 049 super(readers, registry); 050 } 051 052 053 @Override 054 public boolean supportsParameter(MethodParameter parameter) { 055 return parameter.hasParameterAnnotation(RequestBody.class); 056 } 057 058 @Override 059 public Mono<Object> resolveArgument( 060 MethodParameter param, BindingContext bindingContext, ServerWebExchange exchange) { 061 062 RequestBody ann = param.getParameterAnnotation(RequestBody.class); 063 Assert.state(ann != null, "No RequestBody annotation"); 064 return readBody(param, ann.required(), bindingContext, exchange); 065 } 066 067}