Add JSONP support for MappingJackson2MessageConverter

Issue: SPR-9899
This commit is contained in:
Rossen Stoyanchev
2014-05-19 22:10:51 -04:00
parent 05e96ee448
commit 1338d46a6e
8 changed files with 311 additions and 40 deletions

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -27,6 +27,7 @@ import java.util.Map;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.springframework.core.ParameterizedTypeReference;
@@ -35,6 +36,7 @@ import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
@@ -245,13 +247,48 @@ public class MappingJackson2HttpMessageConverterTests {
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");
MappingJacksonValue jsv = new MappingJacksonValue(bean, MyJacksonView1.class);
this.converter.writeInternal(jsv, outputMessage);
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);
this.converter.writeInternal(jacksonValue, outputMessage);
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertTrue(result.contains("\"withView1\":\"with\""));
assertFalse(result.contains("\"withView2\":\"with\""));
assertTrue(result.contains("\"withoutView\":\"without\""));
assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, containsString("\"withoutView\":\"without\""));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
}
@Test
public void jsonp() throws Exception {
MappingJacksonValue jacksonValue = new MappingJacksonValue("foo");
jacksonValue.setSerializationView(MyJacksonView1.class);
jacksonValue.setJsonpFunction("callback");
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.writeInternal(jacksonValue, outputMessage);
assertEquals("callback(\"foo\");", outputMessage.getBodyAsString(Charset.forName("UTF-8")));
}
@Test
public void jsonpAndJsonView() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
JacksonViewBean bean = new JacksonViewBean();
bean.setWithView1("with");
bean.setWithView2("with");
bean.setWithoutView("without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);
jacksonValue.setJsonpFunction("callback");
this.converter.writeInternal(jacksonValue, outputMessage);
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertThat(result, startsWith("callback("));
assertThat(result, endsWith(");"));
assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, containsString("\"withoutView\":\"without\""));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
}

View File

@@ -220,8 +220,9 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
MySampleBean bean = new MySampleBean("with", "with", "without");
MappingJacksonValue jsv = new MappingJacksonValue(bean, MyJacksonView1.class);
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jsv);
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jacksonValue);
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
assertTrue(s.contains("\"with1\":\"with\""));
assertFalse(s.contains("\"with2\":\"with\""));