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