SPR-7031 - MappingJacksonJsonView should add headers for no-caching
This commit is contained in:
parent
f42d4241b4
commit
6f2062fb3a
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
|
|
@ -61,6 +61,8 @@ public class MappingJacksonJsonView extends AbstractView {
|
|||
|
||||
private Set<String> renderedAttributes;
|
||||
|
||||
private boolean disableCaching = true;
|
||||
|
||||
/**
|
||||
* Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}.
|
||||
*/
|
||||
|
|
@ -109,10 +111,24 @@ public class MappingJacksonJsonView extends AbstractView {
|
|||
this.renderedAttributes = renderedAttributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables caching of the generated JSON.
|
||||
*
|
||||
* <p>Default is {@code true}, which will prevent the client from caching the generated JSON.
|
||||
*/
|
||||
public void setDisableCaching(boolean disableCaching) {
|
||||
this.disableCaching = disableCaching;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
|
||||
response.setContentType(getContentType());
|
||||
response.setCharacterEncoding(encoding.getJavaName());
|
||||
if (disableCaching) {
|
||||
response.addHeader("Pragma", "no-cache");
|
||||
response.addHeader("Cache-Control", "no-cache, no-store, max-age=0");
|
||||
response.addDateHeader("Expires", 1L);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
|
|
@ -24,12 +24,12 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.annotate.JsonUseSerializer;
|
||||
import org.codehaus.jackson.map.JsonSerializer;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.map.SerializationConfig;
|
||||
import org.codehaus.jackson.map.SerializerFactory;
|
||||
import org.codehaus.jackson.map.SerializerProvider;
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
import org.codehaus.jackson.map.ser.BeanSerializerFactory;
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
|
@ -79,6 +79,10 @@ public class MappingJacksonJsonViewTest {
|
|||
|
||||
view.render(model, request, response);
|
||||
|
||||
assertEquals("no-cache", response.getHeader("Pragma"));
|
||||
assertEquals("no-cache, no-store, max-age=0", response.getHeader("Cache-Control"));
|
||||
assertNotNull(response.getHeader("Expires"));
|
||||
|
||||
assertEquals(MappingJacksonJsonView.DEFAULT_CONTENT_TYPE, response.getContentType());
|
||||
|
||||
String jsonResult = response.getContentAsString();
|
||||
|
|
@ -87,6 +91,21 @@ public class MappingJacksonJsonViewTest {
|
|||
validateResult();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderCaching() throws Exception {
|
||||
view.setDisableCaching(false);
|
||||
|
||||
Map<String, Object> model = new HashMap<String, Object>();
|
||||
model.put("bindingResult", createMock("binding_result", BindingResult.class));
|
||||
model.put("foo", "bar");
|
||||
|
||||
view.render(model, request, response);
|
||||
|
||||
assertNull(response.getHeader("Pragma"));
|
||||
assertNull(response.getHeader("Cache-Control"));
|
||||
assertNull(response.getHeader("Expires"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderSimpleMapPrefixed() throws Exception {
|
||||
view.setPrefixJson(true);
|
||||
|
|
@ -181,6 +200,7 @@ public class MappingJacksonJsonViewTest {
|
|||
assertNotNull("Json Result did not eval as valid JavaScript", jsResult);
|
||||
}
|
||||
|
||||
|
||||
public static class TestBeanSimple {
|
||||
|
||||
private String value = "foo";
|
||||
|
|
@ -212,7 +232,7 @@ public class MappingJacksonJsonViewTest {
|
|||
}
|
||||
}
|
||||
|
||||
@JsonUseSerializer(TestBeanSimpleSerializer.class)
|
||||
@JsonSerialize(using=TestBeanSimpleSerializer.class)
|
||||
public static class TestBeanSimpleAnnotated extends TestBeanSimple {
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue