Skip to content

Commit 3c8fc46

Browse files
committed
Backported JSON converter alignment (supports method, exception messages)
Includes deprecation of getTypeToken method in GsonHttpMessageConverter. Issue: SPR-15381
1 parent 421fabb commit 3c8fc46

File tree

4 files changed

+45
-60
lines changed

4 files changed

+45
-60
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -29,6 +29,7 @@
2929
* Abstract base class for most {@link GenericHttpMessageConverter} implementations.
3030
*
3131
* @author Sebastien Deleuze
32+
* @author Juergen Hoeller
3233
* @since 4.2
3334
*/
3435
public abstract class AbstractGenericHttpMessageConverter<T> extends AbstractHttpMessageConverter<T>
@@ -58,9 +59,14 @@ protected AbstractGenericHttpMessageConverter(MediaType... supportedMediaTypes)
5859
}
5960

6061

62+
@Override
63+
protected boolean supports(Class<?> clazz) {
64+
return true;
65+
}
66+
6167
@Override
6268
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
63-
return canRead(contextClass, mediaType);
69+
return (type instanceof Class ? canRead((Class<?>) type, mediaType) : canRead(mediaType));
6470
}
6571

6672
@Override
@@ -102,7 +108,6 @@ public HttpHeaders getHeaders() {
102108
}
103109
}
104110

105-
106111
@Override
107112
protected void writeInternal(T t, HttpOutputMessage outputMessage)
108113
throws IOException, HttpMessageNotWritableException {
@@ -113,7 +118,7 @@ protected void writeInternal(T t, HttpOutputMessage outputMessage)
113118
/**
114119
* Abstract template method that writes the actual body. Invoked from {@link #write}.
115120
* @param t the object to write to the output message
116-
* @param type the type of object to write, can be {@code null} if not specified.
121+
* @param type the type of object to write (may be {@code null})
117122
* @param outputMessage the HTTP output message to write to
118123
* @throws IOException in case of I/O errors
119124
* @throws HttpMessageNotWritableException in case of conversion errors

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,24 @@ public ResourceRegionHttpMessageConverter() {
5151

5252

5353
@Override
54-
protected boolean supports(Class<?> clazz) {
55-
// should not be called as we override canRead/canWrite
54+
@SuppressWarnings("unchecked")
55+
protected MediaType getDefaultContentType(Object object) {
56+
if (jafPresent) {
57+
if(object instanceof ResourceRegion) {
58+
return ActivationMediaTypeFactory.getMediaType(((ResourceRegion) object).getResource());
59+
}
60+
else {
61+
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
62+
if(regions.size() > 0) {
63+
return ActivationMediaTypeFactory.getMediaType(regions.iterator().next().getResource());
64+
}
65+
}
66+
}
67+
return MediaType.APPLICATION_OCTET_STREAM;
68+
}
69+
70+
@Override
71+
public boolean canRead(Class<?> clazz, MediaType mediaType) {
5672
return false;
5773
}
5874

@@ -65,31 +81,14 @@ public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
6581
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
6682
throws IOException, HttpMessageNotReadableException {
6783

68-
return null;
84+
throw new UnsupportedOperationException();
6985
}
7086

7187
@Override
7288
protected ResourceRegion readInternal(Class<?> clazz, HttpInputMessage inputMessage)
7389
throws IOException, HttpMessageNotReadableException {
7490

75-
return null;
76-
}
77-
78-
@Override
79-
@SuppressWarnings("unchecked")
80-
protected MediaType getDefaultContentType(Object object) {
81-
if (jafPresent) {
82-
if(object instanceof ResourceRegion) {
83-
return ActivationMediaTypeFactory.getMediaType(((ResourceRegion) object).getResource());
84-
}
85-
else {
86-
Collection<ResourceRegion> regions = (Collection<ResourceRegion>) object;
87-
if(regions.size() > 0) {
88-
return ActivationMediaTypeFactory.getMediaType(regions.iterator().next().getResource());
89-
}
90-
}
91-
}
92-
return MediaType.APPLICATION_OCTET_STREAM;
91+
throw new UnsupportedOperationException();
9392
}
9493

9594
@Override
@@ -100,7 +99,7 @@ public boolean canWrite(Class<?> clazz, MediaType mediaType) {
10099
@Override
101100
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
102101
if (!(type instanceof ParameterizedType)) {
103-
return ResourceRegion.class.isAssignableFrom((Class) type);
102+
return ResourceRegion.class.isAssignableFrom((Class<?>) type);
104103
}
105104
ParameterizedType parameterizedType = (ParameterizedType) type;
106105
if (!(parameterizedType.getRawType() instanceof Class)) {
@@ -140,6 +139,7 @@ protected void writeInternal(Object object, Type type, HttpOutputMessage outputM
140139
}
141140
}
142141

142+
143143
protected void writeResourceRegion(ResourceRegion region, HttpOutputMessage outputMessage) throws IOException {
144144
Assert.notNull(region, "ResourceRegion must not be null");
145145
HttpHeaders responseHeaders = outputMessage.getHeaders();
@@ -197,8 +197,6 @@ private void writeResourceRegionCollection(Collection<ResourceRegion> resourceRe
197197
print(out, "--" + boundaryString + "--");
198198
}
199199

200-
201-
202200
private static void println(OutputStream os) throws IOException {
203201
os.write('\r');
204202
os.write('\n');

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -203,12 +203,6 @@ protected void logWarningIfNecessary(Type type, Throwable cause) {
203203
}
204204
}
205205

206-
@Override
207-
protected boolean supports(Class<?> clazz) {
208-
// should not be called, since we override canRead/Write instead
209-
throw new UnsupportedOperationException();
210-
}
211-
212206
@Override
213207
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
214208
throws IOException, HttpMessageNotReadableException {
@@ -237,7 +231,7 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
237231
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
238232
}
239233
catch (IOException ex) {
240-
throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex);
234+
throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex);
241235
}
242236
}
243237

@@ -289,7 +283,7 @@ else if (filters != null) {
289283

290284
}
291285
catch (JsonProcessingException ex) {
292-
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
286+
throw new HttpMessageNotWritableException("Could not write JSON document: " + ex.getMessage(), ex);
293287
}
294288
}
295289

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -114,34 +114,20 @@ public void setPrefixJson(boolean prefixJson) {
114114

115115

116116
@Override
117-
public boolean canRead(Class<?> clazz, MediaType mediaType) {
118-
return canRead(mediaType);
119-
}
120-
121-
@Override
122-
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
123-
return canWrite(mediaType);
124-
}
125-
126-
@Override
127-
protected boolean supports(Class<?> clazz) {
128-
// should not be called, since we override canRead/Write instead
129-
throw new UnsupportedOperationException();
130-
}
131-
132-
@Override
133-
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
117+
@SuppressWarnings("deprecation")
118+
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
134119
throws IOException, HttpMessageNotReadableException {
135120

136-
TypeToken<?> token = getTypeToken(clazz);
121+
TypeToken<?> token = getTypeToken(type);
137122
return readTypeToken(token, inputMessage);
138123
}
139124

140125
@Override
141-
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
126+
@SuppressWarnings("deprecation")
127+
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
142128
throws IOException, HttpMessageNotReadableException {
143129

144-
TypeToken<?> token = getTypeToken(type);
130+
TypeToken<?> token = getTypeToken(clazz);
145131
return readTypeToken(token, inputMessage);
146132
}
147133

@@ -162,7 +148,9 @@ public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessa
162148
* </pre>
163149
* @param type the type for which to return the TypeToken
164150
* @return the type token
151+
* @deprecated as of Spring Framework 4.3.8, in favor of signature-based resolution
165152
*/
153+
@Deprecated
166154
protected TypeToken<?> getTypeToken(Type type) {
167155
return TypeToken.get(type);
168156
}
@@ -173,7 +161,7 @@ private Object readTypeToken(TypeToken<?> token, HttpInputMessage inputMessage)
173161
return this.gson.fromJson(json, token.getType());
174162
}
175163
catch (JsonParseException ex) {
176-
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
164+
throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex);
177165
}
178166
}
179167

@@ -203,7 +191,7 @@ protected void writeInternal(Object o, Type type, HttpOutputMessage outputMessag
203191
writer.close();
204192
}
205193
catch (JsonIOException ex) {
206-
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
194+
throw new HttpMessageNotWritableException("Could not write JSON document: " + ex.getMessage(), ex);
207195
}
208196
}
209197

0 commit comments

Comments
 (0)