1
1
/*
2
- * Copyright 2002-2010 the original author or authors.
2
+ * Copyright 2002-2011 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .List ;
23
23
24
24
import org .codehaus .jackson .JsonEncoding ;
25
- import org .codehaus .jackson .JsonGenerator ;
26
25
import org .codehaus .jackson .JsonGenerationException ;
26
+ import org .codehaus .jackson .JsonGenerator ;
27
27
import org .codehaus .jackson .JsonParseException ;
28
28
import org .codehaus .jackson .map .JsonMappingException ;
29
29
import org .codehaus .jackson .map .ObjectMapper ;
@@ -68,39 +68,49 @@ public MappingJacksonHttpMessageConverter() {
68
68
}
69
69
70
70
/**
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
72
72
* {@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.
77
78
*/
78
79
public void setObjectMapper (ObjectMapper objectMapper ) {
79
- Assert .notNull (objectMapper , "'objectMapper' must not be null" );
80
+ Assert .notNull (objectMapper , "ObjectMapper must not be null" );
80
81
this .objectMapper = objectMapper ;
81
82
}
82
83
83
84
/**
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.
88
97
*/
89
98
public void setPrefixJson (boolean prefixJson ) {
90
99
this .prefixJson = prefixJson ;
91
100
}
92
101
102
+
93
103
@ Override
94
104
public boolean canRead (Class <?> clazz , MediaType mediaType ) {
95
105
JavaType javaType = getJavaType (clazz );
96
- return this .objectMapper .canDeserialize (javaType ) && canRead (mediaType );
106
+ return ( this .objectMapper .canDeserialize (javaType ) && canRead (mediaType ) );
97
107
}
98
108
99
109
/**
100
110
* 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:
104
114
* <pre class="code">
105
115
* protected JavaType getJavaType(Class<?> clazz) {
106
116
* if (List.class.isAssignableFrom(clazz)) {
@@ -110,7 +120,6 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
110
120
* }
111
121
* }
112
122
* </pre>
113
- *
114
123
* @param clazz the class to return the java type for
115
124
* @return the java type
116
125
*/
@@ -120,7 +129,7 @@ protected JavaType getJavaType(Class<?> clazz) {
120
129
121
130
@ Override
122
131
public boolean canWrite (Class <?> clazz , MediaType mediaType ) {
123
- return this .objectMapper .canSerialize (clazz ) && canWrite (mediaType );
132
+ return ( this .objectMapper .canSerialize (clazz ) && canWrite (mediaType ) );
124
133
}
125
134
126
135
@ Override
@@ -132,6 +141,7 @@ protected boolean supports(Class<?> clazz) {
132
141
@ Override
133
142
protected Object readInternal (Class <?> clazz , HttpInputMessage inputMessage )
134
143
throws IOException , HttpMessageNotReadableException {
144
+
135
145
JavaType javaType = getJavaType (clazz );
136
146
try {
137
147
return this .objectMapper .readValue (inputMessage .getBody (), javaType );
0 commit comments