From ae43b17fa0f7c6303a53a97803a8a64ce3d42b01 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 29 Sep 2014 22:45:10 +0200 Subject: [PATCH] JsonViewResponseBodyAdvice throws IllegalArgumentException in case of >1 view class specified Issue: SPR-12270 --- .../JsonViewResponseBodyAdvice.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java index a53160c37d..170ecc0937 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/JsonViewResponseBodyAdvice.java @@ -17,22 +17,27 @@ package org.springframework.web.servlet.mvc.method.annotation; import com.fasterxml.jackson.annotation.JsonView; + import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJacksonValue; import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; -import org.springframework.util.Assert; /** * A {@code ResponseBodyAdvice} implementation that adds support for * Jackson's {@code @JsonView} annotation declared on a Spring MVC - * {@code @RequestMapping} or {@code @ExceptionHandler} method. The serialization - * view specified in the annotation will be passed in to the - * {@code MappingJackson2HttpMessageConverter} which will then use it to + * {@code @RequestMapping} or {@code @ExceptionHandler} method. + * + *

The serialization view specified in the annotation will be passed in to + * the {@code MappingJackson2HttpMessageConverter} which will then use it to * serialize the response body with. * + *

Note that despite {@code @JsonView} allowing for more than one class to + * be specified, the use for a response body advice is only supported with + * exactly one class argument. Consider the use of a composite interface. + * * @author Rossen Stoyanchev * @since 4.1 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) @@ -49,8 +54,12 @@ public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBo MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { JsonView annotation = returnType.getMethodAnnotation(JsonView.class); - Assert.isTrue(annotation.value().length != 0, "No view class in JsonView annotation on " + returnType); - bodyContainer.setSerializationView(annotation.value()[0]); + Class[] classes = annotation.value(); + if (classes.length != 1) { + throw new IllegalArgumentException( + "@JsonView only supported for response body advice with exactly 1 class argument: " + returnType); + } + bodyContainer.setSerializationView(classes[0]); } }