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