Browse Source
Spring MVC now supports Jackon's serialization views for rendering different subsets of the same POJO from different controller methods (e.g. detailed page vs summary view). Issue: SPR-7156pull/605/head
15 changed files with 610 additions and 16 deletions
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* 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.http.converter.json; |
||||
|
||||
/** |
||||
* Holds an Object to be serialized via Jackson together with a serialization |
||||
* view to be applied. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
* |
||||
* @see com.fasterxml.jackson.annotation.JsonView |
||||
*/ |
||||
public class MappingJacksonValueHolder { |
||||
|
||||
private final Object value; |
||||
|
||||
private final Class<?> serializationView; |
||||
|
||||
|
||||
/** |
||||
* Create a new instance. |
||||
* @param value the Object to be serialized |
||||
* @param serializationView the view to be applied |
||||
*/ |
||||
public MappingJacksonValueHolder(Object value, Class<?> serializationView) { |
||||
this.value = value; |
||||
this.serializationView = serializationView; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Return the value to be serialized. |
||||
*/ |
||||
public Object getValue() { |
||||
return this.value; |
||||
} |
||||
|
||||
/** |
||||
* Return the serialization view to use. |
||||
*/ |
||||
public Class<?> getSerializationView() { |
||||
return this.serializationView; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue