Add option to set Content-Length in JSON Views

MappingJackson2JsonView and MappingJacksonJsonView now provide an
option that will set the Content-Length header of JSON responses.
Use of the option implies buffering of the response and it must be
enabled explicitly.

Issue: SPR-7866
This commit is contained in:
Rossen Stoyanchev
2012-05-15 18:06:14 -04:00
parent 2017b24867
commit 01a9dd9772
5 changed files with 53 additions and 9 deletions

View File

@@ -59,7 +59,7 @@ import com.fasterxml.jackson.databind.ser.Serializers;
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class MappingJackson2JsonViewTest {
public class MappingJackson2JsonViewTests {
private MappingJackson2JsonView view;
@@ -94,6 +94,7 @@ public class MappingJackson2JsonViewTest {
model.put("bindingResult", createMock("binding_result", BindingResult.class));
model.put("foo", "bar");
view.setUpdateContentLength(true);
view.render(model, request, response);
assertEquals("no-cache", response.getHeader("Pragma"));
@@ -104,6 +105,7 @@ public class MappingJackson2JsonViewTest {
String jsonResult = response.getContentAsString();
assertTrue(jsonResult.length() > 0);
assertEquals(jsonResult.length(), response.getContentLength());
validateResult();
}
@@ -137,9 +139,11 @@ public class MappingJackson2JsonViewTest {
model.put("bindingResult", createMock("binding_result", BindingResult.class));
model.put("foo", bean);
view.setUpdateContentLength(true);
view.render(model, request, response);
assertTrue(response.getContentAsString().length() > 0);
assertEquals(response.getContentAsString().length(), response.getContentLength());
validateResult();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 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.
@@ -52,7 +52,7 @@ import org.springframework.validation.BindingResult;
* @author Jeremy Grelle
* @author Arjen Poutsma
*/
public class MappingJacksonJsonViewTest {
public class MappingJacksonJsonViewTests {
private MappingJacksonJsonView view;
@@ -87,6 +87,7 @@ public class MappingJacksonJsonViewTest {
model.put("bindingResult", createMock("binding_result", BindingResult.class));
model.put("foo", "bar");
view.setUpdateContentLength(true);
view.render(model, request, response);
assertEquals("no-cache", response.getHeader("Pragma"));
@@ -97,6 +98,7 @@ public class MappingJacksonJsonViewTest {
String jsonResult = response.getContentAsString();
assertTrue(jsonResult.length() > 0);
assertEquals(jsonResult.length(), response.getContentLength());
validateResult();
}
@@ -130,9 +132,11 @@ public class MappingJacksonJsonViewTest {
model.put("bindingResult", createMock("binding_result", BindingResult.class));
model.put("foo", bean);
view.setUpdateContentLength(true);
view.render(model, request, response);
assertTrue(response.getContentAsString().length() > 0);
assertEquals(response.getContentAsString().length(), response.getContentLength());
validateResult();
}