Skip to content

Commit 3f7dccd

Browse files
committed
added getObjectMapper() accessor to MappingJacksonHttpMessageConverter (SPR-8605)
1 parent ac52433 commit 3f7dccd

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 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.
@@ -22,8 +22,8 @@
2222
import java.util.List;
2323

2424
import org.codehaus.jackson.JsonEncoding;
25-
import org.codehaus.jackson.JsonGenerator;
2625
import org.codehaus.jackson.JsonGenerationException;
26+
import org.codehaus.jackson.JsonGenerator;
2727
import org.codehaus.jackson.JsonParseException;
2828
import org.codehaus.jackson.map.JsonMappingException;
2929
import org.codehaus.jackson.map.ObjectMapper;
@@ -68,39 +68,49 @@ public MappingJacksonHttpMessageConverter() {
6868
}
6969

7070
/**
71-
* Sets the {@code ObjectMapper} for this view. If not set, a default
71+
* Set the {@code ObjectMapper} for this view. If not set, a default
7272
* {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
73-
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON serialization
74-
* process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory} can be configured that provides
75-
* custom serializers for specific types. The other option for refining the serialization process is to use Jackson's
76-
* provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.
73+
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON
74+
* serialization process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory}
75+
* can be configured that provides custom serializers for specific types. The other option for refining
76+
* the serialization process is to use Jackson's provided annotations on the types to be serialized,
77+
* in which case a custom-configured ObjectMapper is unnecessary.
7778
*/
7879
public void setObjectMapper(ObjectMapper objectMapper) {
79-
Assert.notNull(objectMapper, "'objectMapper' must not be null");
80+
Assert.notNull(objectMapper, "ObjectMapper must not be null");
8081
this.objectMapper = objectMapper;
8182
}
8283

8384
/**
84-
* Indicates whether the JSON output by this view should be prefixed with "{} &&". Default is false.
85-
* <p> Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string
86-
* syntactically invalid as a script so that it cannot be hijacked. This prefix does not affect the evaluation of JSON,
87-
* but if JSON validation is performed on the string, the prefix would need to be ignored.
85+
* Return the underlying {@code ObjectMapper} for this view.
86+
*/
87+
public ObjectMapper getObjectMapper() {
88+
return this.objectMapper;
89+
}
90+
91+
/**
92+
* Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
93+
* <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
94+
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
95+
* This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
96+
* string, the prefix would need to be ignored.
8897
*/
8998
public void setPrefixJson(boolean prefixJson) {
9099
this.prefixJson = prefixJson;
91100
}
92101

102+
93103
@Override
94104
public boolean canRead(Class<?> clazz, MediaType mediaType) {
95105
JavaType javaType = getJavaType(clazz);
96-
return this.objectMapper.canDeserialize(javaType) && canRead(mediaType);
106+
return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));
97107
}
98108

99109
/**
100110
* Returns the Jackson {@link JavaType} for the specific class.
101-
*
102-
* <p>Default implementation returns {@link TypeFactory#type(java.lang.reflect.Type)}, but this can be overridden
103-
* in subclasses, to allow for custom generic collection handling. For instance:
111+
* <p>The default implementation returns {@link TypeFactory#type(java.lang.reflect.Type)},
112+
* but this can be overridden in subclasses, to allow for custom generic collection handling.
113+
* For instance:
104114
* <pre class="code">
105115
* protected JavaType getJavaType(Class&lt;?&gt; clazz) {
106116
* if (List.class.isAssignableFrom(clazz)) {
@@ -110,7 +120,6 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
110120
* }
111121
* }
112122
* </pre>
113-
*
114123
* @param clazz the class to return the java type for
115124
* @return the java type
116125
*/
@@ -120,7 +129,7 @@ protected JavaType getJavaType(Class<?> clazz) {
120129

121130
@Override
122131
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
123-
return this.objectMapper.canSerialize(clazz) && canWrite(mediaType);
132+
return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType));
124133
}
125134

126135
@Override
@@ -132,6 +141,7 @@ protected boolean supports(Class<?> clazz) {
132141
@Override
133142
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
134143
throws IOException, HttpMessageNotReadableException {
144+
135145
JavaType javaType = getJavaType(clazz);
136146
try {
137147
return this.objectMapper.readValue(inputMessage.getBody(), javaType);

0 commit comments

Comments
 (0)