JsonViewResponseBodyAdvice throws IllegalArgumentException in case of >1 view class specified
Issue: SPR-12270
This commit is contained in:
parent
7f43f02a13
commit
ae43b17fa0
|
@ -17,22 +17,27 @@
|
||||||
package org.springframework.web.servlet.mvc.method.annotation;
|
package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@code ResponseBodyAdvice} implementation that adds support for
|
* A {@code ResponseBodyAdvice} implementation that adds support for
|
||||||
* Jackson's {@code @JsonView} annotation declared on a Spring MVC
|
* Jackson's {@code @JsonView} annotation declared on a Spring MVC
|
||||||
* {@code @RequestMapping} or {@code @ExceptionHandler} method. The serialization
|
* {@code @RequestMapping} or {@code @ExceptionHandler} method.
|
||||||
* view specified in the annotation will be passed in to the
|
*
|
||||||
* {@code MappingJackson2HttpMessageConverter} which will then use it to
|
* <p>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.
|
* serialize the response body with.
|
||||||
*
|
*
|
||||||
|
* <p>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
|
* @author Rossen Stoyanchev
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||||
|
@ -49,8 +54,12 @@ public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBo
|
||||||
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
|
|
||||||
JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
|
JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
|
||||||
Assert.isTrue(annotation.value().length != 0, "No view class in JsonView annotation on " + returnType);
|
Class<?>[] classes = annotation.value();
|
||||||
bodyContainer.setSerializationView(annotation.value()[0]);
|
if (classes.length != 1) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
|
||||||
|
}
|
||||||
|
bodyContainer.setSerializationView(classes[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue