Add JSONP support for MappingJackson2MessageConverter
Issue: SPR-9899
This commit is contained in:
@@ -249,15 +249,27 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
|
||||
if (this.jsonPrefix != null) {
|
||||
jsonGenerator.writeRaw(this.jsonPrefix);
|
||||
}
|
||||
Class<?> serializationView = null;
|
||||
String jsonpFunction = null;
|
||||
if (object instanceof MappingJacksonValue) {
|
||||
MappingJacksonValue valueHolder = (MappingJacksonValue) object;
|
||||
object = valueHolder.getValue();
|
||||
Class<?> serializationView = valueHolder.getSerializationView();
|
||||
MappingJacksonValue container = (MappingJacksonValue) object;
|
||||
object = container.getValue();
|
||||
serializationView = container.getSerializationView();
|
||||
jsonpFunction = container.getJsonpFunction();
|
||||
}
|
||||
if (jsonpFunction != null) {
|
||||
jsonGenerator.writeRaw(jsonpFunction + "(" );
|
||||
}
|
||||
if (serializationView != null) {
|
||||
this.objectMapper.writerWithView(serializationView).writeValue(jsonGenerator, object);
|
||||
}
|
||||
else {
|
||||
this.objectMapper.writeValue(jsonGenerator, object);
|
||||
}
|
||||
if (jsonpFunction != null) {
|
||||
jsonGenerator.writeRaw(");");
|
||||
jsonGenerator.flush();
|
||||
}
|
||||
}
|
||||
catch (JsonProcessingException ex) {
|
||||
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
|
||||
|
||||
@@ -17,44 +17,83 @@
|
||||
package org.springframework.http.converter.json;
|
||||
|
||||
/**
|
||||
* Holds an Object to be serialized via Jackson together with a serialization
|
||||
* view to be applied.
|
||||
* A simple holder for the POJO to serialize via
|
||||
* {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
|
||||
* MappingJackson2HttpMessageConverter} along with further serialization
|
||||
* instructions to be passed in to the converter.
|
||||
*
|
||||
* <p>On the server side this wrapper is added with a
|
||||
* {@code ResponseBodyInterceptor} after content negotiation selects the
|
||||
* converter to use but before the write.
|
||||
*
|
||||
* <p>On the client side, simply wrap the POJO and pass it in to the
|
||||
* {@code RestTemplate}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*
|
||||
* @see com.fasterxml.jackson.annotation.JsonView
|
||||
*/
|
||||
public class MappingJacksonValue {
|
||||
|
||||
private final Object value;
|
||||
private Object value;
|
||||
|
||||
private final Class<?> serializationView;
|
||||
private Class<?> serializationView;
|
||||
|
||||
private String jsonpFunction;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
* Create a new instance wrapping the given POJO to be serialized.
|
||||
* @param value the Object to be serialized
|
||||
* @param serializationView the view to be applied
|
||||
*/
|
||||
public MappingJacksonValue(Object value, Class<?> serializationView) {
|
||||
public MappingJacksonValue(Object value) {
|
||||
this.value = value;
|
||||
this.serializationView = serializationView;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the value to be serialized.
|
||||
* Modify the POJO to serialize.
|
||||
*/
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the POJO that needs to be serialized.
|
||||
*/
|
||||
public Object getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the serialization view to serialize the POJO with.
|
||||
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||
* @see com.fasterxml.jackson.annotation.JsonView
|
||||
*/
|
||||
public void setSerializationView(Class<?> serializationView) {
|
||||
this.serializationView = serializationView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the serialization view to use.
|
||||
* @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class)
|
||||
* @see com.fasterxml.jackson.annotation.JsonView
|
||||
*/
|
||||
public Class<?> getSerializationView() {
|
||||
return this.serializationView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the JSONP function name.
|
||||
*/
|
||||
public void setJsonpFunction(String functionName) {
|
||||
this.jsonpFunction = functionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured JSONP function name.
|
||||
*/
|
||||
public String getJsonpFunction() {
|
||||
return this.jsonpFunction;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user