Skip to content

Commit fcb0cf2

Browse files
jhoellerunknown
authored andcommitted
MappingJackson(2)HttpMessageConverter alignment
1 parent a0c377b commit fcb0cf2

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.lang.reflect.Type;
2121
import java.nio.charset.Charset;
22-
import java.util.List;
2322

2423
import com.fasterxml.jackson.core.JsonEncoding;
2524
import com.fasterxml.jackson.core.JsonGenerator;
@@ -45,13 +44,14 @@
4544
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
4645
*
4746
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the
48-
* {@link #setSupportedMediaTypes(List) supportedMediaTypes} property.
47+
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
48+
*
49+
* <p>Tested against Jackson 2.2; compatible with Jackson 2.0 and higher.
4950
*
5051
* @author Arjen Poutsma
5152
* @author Keith Donald
5253
* @author Rossen Stoyanchev
5354
* @since 3.1.2
54-
* @see org.springframework.web.servlet.view.json.MappingJackson2JsonView
5555
*/
5656
public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object>
5757
implements GenericHttpMessageConverter<Object> {
@@ -70,12 +70,14 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv
7070
* Construct a new {@code MappingJackson2HttpMessageConverter}.
7171
*/
7272
public MappingJackson2HttpMessageConverter() {
73-
super(new MediaType("application", "json", DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET));
73+
super(new MediaType("application", "json", DEFAULT_CHARSET),
74+
new MediaType("application", "*+json", DEFAULT_CHARSET));
7475
}
7576

77+
7678
/**
77-
* Set the {@code ObjectMapper} for this view. If not set, a default
78-
* {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
79+
* Set the {@code ObjectMapper} for this view.
80+
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
7981
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON
8082
* serialization process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory}
8183
* can be configured that provides custom serializers for specific types. The other option for refining
@@ -88,12 +90,6 @@ public void setObjectMapper(ObjectMapper objectMapper) {
8890
configurePrettyPrint();
8991
}
9092

91-
private void configurePrettyPrint() {
92-
if (this.prettyPrint != null) {
93-
this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint);
94-
}
95-
}
96-
9793
/**
9894
* Return the underlying {@code ObjectMapper} for this view.
9995
*/
@@ -126,6 +122,12 @@ public void setPrettyPrint(boolean prettyPrint) {
126122
configurePrettyPrint();
127123
}
128124

125+
private void configurePrettyPrint() {
126+
if (this.prettyPrint != null) {
127+
this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint);
128+
}
129+
}
130+
129131

130132
@Override
131133
public boolean canRead(Class<?> clazz, MediaType mediaType) {
@@ -172,14 +174,14 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
172174
}
173175
}
174176

175-
176177
@Override
177178
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
178179
throws IOException, HttpMessageNotWritableException {
179180

180181
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
181182
// The following has been deprecated as late as Jackson 2.2 (April 2013);
182183
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
184+
@SuppressWarnings("deprecation")
183185
JsonGenerator jsonGenerator =
184186
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
185187

spring-web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.lang.reflect.Type;
2121
import java.nio.charset.Charset;
22-
import java.util.List;
2322

2423
import org.codehaus.jackson.JsonEncoding;
2524
import org.codehaus.jackson.JsonGenerator;
@@ -44,11 +43,13 @@
4443
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
4544
*
4645
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the
47-
* {@link #setSupportedMediaTypes(List) supportedMediaTypes} property.
46+
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
47+
*
48+
* <p><b>NOTE:</b> Requires Jackson 1.8 or higher, as of Spring 4.0.
4849
*
4950
* @author Arjen Poutsma
51+
* @author Juergen Hoeller
5052
* @since 3.0
51-
* @see org.springframework.web.servlet.view.json.MappingJacksonJsonView
5253
*/
5354
public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConverter<Object>
5455
implements GenericHttpMessageConverter<Object> {
@@ -67,12 +68,14 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
6768
* Construct a new {@code MappingJacksonHttpMessageConverter}.
6869
*/
6970
public MappingJacksonHttpMessageConverter() {
70-
super(new MediaType("application", "json", DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET));
71+
super(new MediaType("application", "json", DEFAULT_CHARSET),
72+
new MediaType("application", "*+json", DEFAULT_CHARSET));
7173
}
7274

75+
7376
/**
74-
* Set the {@code ObjectMapper} for this view. If not set, a default
75-
* {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
77+
* Set the {@code ObjectMapper} for this view.
78+
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
7679
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON
7780
* serialization process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory}
7881
* can be configured that provides custom serializers for specific types. The other option for refining
@@ -85,12 +88,6 @@ public void setObjectMapper(ObjectMapper objectMapper) {
8588
configurePrettyPrint();
8689
}
8790

88-
private void configurePrettyPrint() {
89-
if (this.prettyPrint != null) {
90-
this.objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, this.prettyPrint);
91-
}
92-
}
93-
9491
/**
9592
* Return the underlying {@code ObjectMapper} for this view.
9693
*/
@@ -124,6 +121,12 @@ public void setPrettyPrint(boolean prettyPrint) {
124121
configurePrettyPrint();
125122
}
126123

124+
private void configurePrettyPrint() {
125+
if (this.prettyPrint != null) {
126+
this.objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, this.prettyPrint);
127+
}
128+
}
129+
127130

128131
@Override
129132
public boolean canRead(Class<?> clazz, MediaType mediaType) {

spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@
4242
* using <a href="http://jackson.codehaus.org/">Jackson 2.x's</a> {@link ObjectMapper}.
4343
*
4444
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes)
45-
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
46-
* alone via {@link #setExtractValueFromSingleKeyModel}.
45+
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as
46+
* JSON alone via {@link #setExtractValueFromSingleKeyModel}.
4747
*
4848
* @author Jeremy Grelle
4949
* @author Arjen Poutsma
5050
* @author Rossen Stoyanchev
5151
* @author Juergen Hoeller
5252
* @since 3.1.2
53-
* @see org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
5453
*/
5554
public class MappingJackson2JsonView extends AbstractView {
5655

@@ -280,6 +279,7 @@ protected Object filterModel(Map<String, Object> model) {
280279
protected void writeContent(OutputStream stream, Object value, boolean prefixJson) throws IOException {
281280
// The following has been deprecated as late as Jackson 2.2 (April 2013);
282281
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
282+
@SuppressWarnings("deprecation")
283283
JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding);
284284

285285
// A workaround for JsonGenerators not applying serialization features

spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJacksonJsonView.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@
4242
* using <a href="http://jackson.codehaus.org/">Jackson 1.x's</a> {@link ObjectMapper}.
4343
*
4444
* <p>By default, the entire contents of the model map (with the exception of framework-specific classes)
45-
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as JSON
46-
* alone via {@link #setExtractValueFromSingleKeyModel}.
45+
* will be encoded as JSON. If the model contains only one key, you can have it extracted encoded as
46+
* JSON alone via {@link #setExtractValueFromSingleKeyModel}.
4747
*
4848
* @author Jeremy Grelle
4949
* @author Arjen Poutsma
5050
* @author Rossen Stoyanchev
5151
* @author Juergen Hoeller
5252
* @since 3.0
53-
* @see org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
5453
*/
5554
public class MappingJacksonJsonView extends AbstractView {
5655

0 commit comments

Comments
 (0)