001/*
002 * Copyright 2002-2015 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.servlet.mvc.method.annotation;
018
019import java.io.IOException;
020import java.lang.reflect.Type;
021
022import com.fasterxml.jackson.annotation.JsonView;
023
024import org.springframework.core.MethodParameter;
025import org.springframework.http.HttpInputMessage;
026import org.springframework.http.converter.HttpMessageConverter;
027import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
028import org.springframework.http.converter.json.MappingJacksonInputMessage;
029
030/**
031 * A {@link RequestBodyAdvice} implementation that adds support for Jackson's
032 * {@code @JsonView} annotation declared on a Spring MVC {@code @HttpEntity}
033 * or {@code @RequestBody} method parameter.
034 *
035 * <p>The deserialization view specified in the annotation will be passed in to the
036 * {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter}
037 * which will then use it to deserialize the request body with.
038 *
039 * <p>Note that despite {@code @JsonView} allowing for more than one class to
040 * be specified, the use for a request body advice is only supported with
041 * exactly one class argument. Consider the use of a composite interface.
042 *
043 * <p>Jackson 2.5 or later is required for parameter-level use of {@code @JsonView}.
044 *
045 * @author Sebastien Deleuze
046 * @since 4.2
047 * @see com.fasterxml.jackson.annotation.JsonView
048 * @see com.fasterxml.jackson.databind.ObjectMapper#readerWithView(Class)
049 */
050public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter {
051
052        @Override
053        public boolean supports(MethodParameter methodParameter, Type targetType,
054                        Class<? extends HttpMessageConverter<?>> converterType) {
055
056                return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
057                                methodParameter.getParameterAnnotation(JsonView.class) != null);
058        }
059
060        @Override
061        public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
062                        Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {
063
064                JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);
065                Class<?>[] classes = annotation.value();
066                if (classes.length != 1) {
067                        throw new IllegalArgumentException(
068                                        "@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
069                }
070                return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
071        }
072
073}