/*
* Copyright 2002-2017 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.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.lang.reflect.Type;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
Allows customizing the request before its body is read and converted into an Object and also allows for processing of the resulting Object before it is passed into a controller method as an @RequestBody
or an HttpEntity
method argument. Implementations of this contract may be registered directly with the RequestMappingHandlerAdapter
or more likely annotated with @ControllerAdvice
in which case they are auto-detected.
Author: Rossen Stoyanchev Since: 4.2
/**
* Allows customizing the request before its body is read and converted into an
* Object and also allows for processing of the resulting Object before it is
* passed into a controller method as an {@code @RequestBody} or an
* {@code HttpEntity} method argument.
*
* <p>Implementations of this contract may be registered directly with the
* {@code RequestMappingHandlerAdapter} or more likely annotated with
* {@code @ControllerAdvice} in which case they are auto-detected.
*
* @author Rossen Stoyanchev
* @since 4.2
*/
public interface RequestBodyAdvice {
Invoked first to determine if this interceptor applies.
Params: - methodParameter – the method parameter
- targetType – the target type, not necessarily the same as the method parameter type, e.g. for
HttpEntity<String>
. - converterType – the selected converter type
Returns: whether this interceptor should be invoked or not
/**
* Invoked first to determine if this interceptor applies.
* @param methodParameter the method parameter
* @param targetType the target type, not necessarily the same as the method
* parameter type, e.g. for {@code HttpEntity<String>}.
* @param converterType the selected converter type
* @return whether this interceptor should be invoked or not
*/
boolean supports(MethodParameter methodParameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType);
Invoked second before the request body is read and converted.
Params: - inputMessage – the request
- parameter – the target method parameter
- targetType – the target type, not necessarily the same as the method parameter type, e.g. for
HttpEntity<String>
. - converterType – the converter used to deserialize the body
Returns: the input request or a new instance, never null
/**
* Invoked second before the request body is read and converted.
* @param inputMessage the request
* @param parameter the target method parameter
* @param targetType the target type, not necessarily the same as the method
* parameter type, e.g. for {@code HttpEntity<String>}.
* @param converterType the converter used to deserialize the body
* @return the input request or a new instance, never {@code null}
*/
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;
Invoked third (and last) after the request body is converted to an Object.
Params: - body – set to the converter Object before the first advice is called
- inputMessage – the request
- parameter – the target method parameter
- targetType – the target type, not necessarily the same as the method parameter type, e.g. for
HttpEntity<String>
. - converterType – the converter used to deserialize the body
Returns: the same body or a new instance
/**
* Invoked third (and last) after the request body is converted to an Object.
* @param body set to the converter Object before the first advice is called
* @param inputMessage the request
* @param parameter the target method parameter
* @param targetType the target type, not necessarily the same as the method
* parameter type, e.g. for {@code HttpEntity<String>}.
* @param converterType the converter used to deserialize the body
* @return the same body or a new instance
*/
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
Invoked second (and last) if the body is empty.
Params: - body – usually set to
null
before the first advice is called - inputMessage – the request
- parameter – the method parameter
- targetType – the target type, not necessarily the same as the method parameter type, e.g. for
HttpEntity<String>
. - converterType – the selected converter type
Returns: the value to use or null
which may then raise an HttpMessageNotReadableException
if the argument is required.
/**
* Invoked second (and last) if the body is empty.
* @param body usually set to {@code null} before the first advice is called
* @param inputMessage the request
* @param parameter the method parameter
* @param targetType the target type, not necessarily the same as the method
* parameter type, e.g. for {@code HttpEntity<String>}.
* @param converterType the selected converter type
* @return the value to use or {@code null} which may then raise an
* {@code HttpMessageNotReadableException} if the argument is required.
*/
@Nullable
Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType);
}