Browse Source
The newly added support for ResponseBodyInterceptor is a good fit for the (also recently added) support for the Jackson @JsonView annotation. This change refactors the original implementation of @JsonView support for @ResponseBody and ResponseEntity controller methods this time implemented as an ResponseBodyInterceptor. Issue: SPR-7156pull/493/merge
17 changed files with 321 additions and 134 deletions
@ -1,62 +0,0 @@
@@ -1,62 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.http.converter; |
||||
|
||||
import org.springframework.core.MethodParameter; |
||||
import org.springframework.http.HttpInputMessage; |
||||
import org.springframework.http.HttpOutputMessage; |
||||
import org.springframework.http.MediaType; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* An HttpMessageConverter that supports converting the value returned from a |
||||
* method by incorporating {@link org.springframework.core.MethodParameter} |
||||
* information into the conversion. Such a converter can for example take into |
||||
* account information from method-level annotations. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public interface MethodParameterHttpMessageConverter<T> extends HttpMessageConverter<T> { |
||||
|
||||
/** |
||||
* This method mirrors {@link HttpMessageConverter#canRead(Class, MediaType)} |
||||
* with an additional {@code MethodParameter}. |
||||
*/ |
||||
boolean canRead(Class<?> clazz, MediaType mediaType, MethodParameter parameter); |
||||
|
||||
/** |
||||
* This method mirrors {@link HttpMessageConverter#canWrite(Class, MediaType)} |
||||
* with an additional {@code MethodParameter}. |
||||
*/ |
||||
boolean canWrite(Class<?> clazz, MediaType mediaType, MethodParameter parameter); |
||||
|
||||
/** |
||||
* This method mirrors {@link HttpMessageConverter#read(Class, HttpInputMessage)} |
||||
* with an additional {@code MethodParameter}. |
||||
*/ |
||||
T read(Class<? extends T> clazz, HttpInputMessage inputMessage, MethodParameter parameter) |
||||
throws IOException, HttpMessageNotReadableException; |
||||
|
||||
/** |
||||
* This method mirrors {@link HttpMessageConverter#write(Object, MediaType, HttpOutputMessage)} |
||||
* with an additional {@code MethodParameter}. |
||||
*/ |
||||
void write(T t, MediaType contentType, HttpOutputMessage outputMessage, MethodParameter parameter) |
||||
throws IOException, HttpMessageNotWritableException; |
||||
|
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
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.MappingJackson2HttpMessageConverter; |
||||
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 ResponseBodyInterceptor} implementation that adds support for the |
||||
* Jackson {@code @JsonView} annotation on a Spring MVC {@code @RequestMapping} |
||||
* or {@code @ExceptionHandler} method. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public class JsonViewResponseBodyInterceptor implements ResponseBodyInterceptor { |
||||
|
||||
|
||||
@Override |
||||
@SuppressWarnings("unchecked") |
||||
public <T> T beforeBodyWrite(T body, MediaType contentType, Class<? extends HttpMessageConverter<T>> converterType, |
||||
MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { |
||||
|
||||
if (!MappingJackson2HttpMessageConverter.class.equals(converterType)) { |
||||
return body; |
||||
} |
||||
|
||||
JsonView annotation = returnType.getMethodAnnotation(JsonView.class); |
||||
if (annotation == null) { |
||||
return body; |
||||
} |
||||
|
||||
Assert.isTrue(annotation.value().length != 0, |
||||
"Expected at least one serialization view class in JsonView annotation on " + returnType); |
||||
|
||||
return (T) new MappingJacksonValue(body, annotation.value()[0]); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue