001/*
002 * Copyright 2002-2017 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 com.fasterxml.jackson.annotation.JsonView;
020
021import org.springframework.core.MethodParameter;
022import org.springframework.http.MediaType;
023import org.springframework.http.converter.HttpMessageConverter;
024import org.springframework.http.converter.json.MappingJacksonValue;
025import org.springframework.http.server.ServerHttpRequest;
026import org.springframework.http.server.ServerHttpResponse;
027import org.springframework.util.Assert;
028
029/**
030 * A {@link ResponseBodyAdvice} implementation that adds support for Jackson's
031 * {@code @JsonView} annotation declared on a Spring MVC {@code @RequestMapping}
032 * or {@code @ExceptionHandler} method.
033 *
034 * <p>The serialization view specified in the annotation will be passed in to the
035 * {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter}
036 * which will then use it to serialize the response body.
037 *
038 * <p>Note that despite {@code @JsonView} allowing for more than one class to
039 * be specified, the use for a response body advice is only supported with
040 * exactly one class argument. Consider the use of a composite interface.
041 *
042 * @author Rossen Stoyanchev
043 * @since 4.1
044 * @see com.fasterxml.jackson.annotation.JsonView
045 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
046 */
047public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice {
048
049        @Override
050        public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
051                return super.supports(returnType, converterType) && returnType.hasMethodAnnotation(JsonView.class);
052        }
053
054        @Override
055        protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
056                        MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
057
058                JsonView ann = returnType.getMethodAnnotation(JsonView.class);
059                Assert.state(ann != null, "No JsonView annotation");
060
061                Class<?>[] classes = ann.value();
062                if (classes.length != 1) {
063                        throw new IllegalArgumentException(
064                                        "@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
065                }
066
067                bodyContainer.setSerializationView(classes[0]);
068        }
069
070}