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

@ -16,6 +16,8 @@
package org.springframework.web.servlet.view.json;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -71,6 +73,7 @@ public class MappingJackson2JsonView extends AbstractView {
private boolean disableCaching = true;
private boolean updateContentLength = false;
/**
* Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}.
@ -199,6 +202,15 @@ public class MappingJackson2JsonView extends AbstractView {
this.disableCaching = disableCaching;
}
/**
* Whether to update the 'Content-Length' header of the response. When set to
* {@code true}, the response is buffered in order to determine the content
* length and set the 'Content-Length' header of the response.
* <p>The default setting is {@code false}.
*/
public void setUpdateContentLength(boolean updateContentLength) {
this.updateContentLength = updateContentLength;
}
@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
@ -215,9 +227,10 @@ public class MappingJackson2JsonView extends AbstractView {
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
OutputStream stream = this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream();
Object value = filterModel(model);
JsonGenerator generator =
this.objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), this.encoding);
JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding);
// A workaround for JsonGenerators not applying serialization features
// https://github.com/FasterXML/jackson-databind/issues/12
@ -229,6 +242,10 @@ public class MappingJackson2JsonView extends AbstractView {
generator.writeRaw("{} && ");
}
this.objectMapper.writeValue(generator, value);
if (this.updateContentLength) {
writeToResponse(response, (ByteArrayOutputStream) stream);
}
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 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.
@ -16,6 +16,8 @@
package org.springframework.web.servlet.view.json;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -73,6 +75,7 @@ public class MappingJacksonJsonView extends AbstractView {
private boolean disableCaching = true;
private boolean updateContentLength = false;
/**
* Construct a new {@code JacksonJsonView}, setting the content type to {@code application/json}.
@ -201,6 +204,16 @@ public class MappingJacksonJsonView extends AbstractView {
this.disableCaching = disableCaching;
}
/**
* Whether to update the 'Content-Length' header of the response. When set to
* {@code true}, the response is buffered in order to determine the content
* length and set the 'Content-Length' header of the response.
* <p>The default setting is {@code false}.
*/
public void setUpdateContentLength(boolean updateContentLength) {
this.updateContentLength = updateContentLength;
}
@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
@ -217,9 +230,10 @@ public class MappingJacksonJsonView extends AbstractView {
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
OutputStream stream = this.updateContentLength ? createTemporaryOutputStream() : response.getOutputStream();
Object value = filterModel(model);
JsonGenerator generator =
this.objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), this.encoding);
JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding);
// A workaround for JsonGenerators not applying serialization features
// https://github.com/FasterXML/jackson-databind/issues/12
@ -231,6 +245,10 @@ public class MappingJacksonJsonView extends AbstractView {
generator.writeRaw("{} && ");
}
this.objectMapper.writeValue(generator, value);
if (this.updateContentLength) {
writeToResponse(response, (ByteArrayOutputStream) stream);
}
}
/**

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();
}

View File

@ -25,7 +25,8 @@ Changes in version 3.2 M1
* support access to all URI vars via @PathVariable Map<String, String>
* add "excludedExceptions" property to SimpleUrlHandlerMapping
* add CompositeRequestCondition for use with multiple custom request mapping conditions
* add option to configure custom MessageCodesResolver through WebMvcConfigurer
* add ability to configure custom MessageCodesResolver through the MVC Java config
* add option in MappingJacksonJsonView for setting the Content-Length header
Changes in version 3.1.1 (2012-02-16)
-------------------------------------