Skip to content

Commit e366746

Browse files
committed
AbstractJackson2HttpMessageConverter's canRead/canWrite checks media type first before delegating to Jackson
Issue: SPR-14163
1 parent ee31617 commit e366746

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@ protected AbstractHttpMessageConverter(MediaType supportedMediaType) {
7171
}
7272

7373
/**
74-
* Construct an {@code AbstractHttpMessageConverter} with multiple supported media type.
74+
* Construct an {@code AbstractHttpMessageConverter} with multiple supported media types.
7575
* @param supportedMediaTypes the supported media types
7676
*/
7777
protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes) {
7878
setSupportedMediaTypes(Arrays.asList(supportedMediaTypes));
7979
}
8080

81+
/**
82+
* Construct an {@code AbstractHttpMessageConverter} with a default charset and
83+
* multiple supported media types.
84+
* @param defaultCharset the default character set
85+
* @param supportedMediaTypes the supported media types
86+
* @since 4.3
87+
*/
8188
protected AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes) {
8289
this.defaultCharset = defaultCharset;
8390
setSupportedMediaTypes(Arrays.asList(supportedMediaTypes));
@@ -98,16 +105,22 @@ public List<MediaType> getSupportedMediaTypes() {
98105
}
99106

100107
/**
101-
* Set the default character set if any.
108+
* Set the default character set, if any.
109+
* @since 4.3
102110
*/
103111
public void setDefaultCharset(Charset defaultCharset) {
104112
this.defaultCharset = defaultCharset;
105113
}
106114

115+
/**
116+
* Return the default character set, if any.
117+
* @since 4.3
118+
*/
107119
public Charset getDefaultCharset() {
108-
return defaultCharset;
120+
return this.defaultCharset;
109121
}
110122

123+
111124
/**
112125
* This implementation checks if the given class is {@linkplain #supports(Class) supported},
113126
* and if the {@linkplain #getSupportedMediaTypes() supported media types}
@@ -119,7 +132,7 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
119132
}
120133

121134
/**
122-
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List)
135+
* Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List)
123136
* supported} media types {@link MediaType#includes(MediaType) include} the
124137
* given media type.
125138
* @param mediaType the media type to read, can be {@code null} if not specified.

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
140140
@Override
141141
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
142142
JavaType javaType = getJavaType(type, contextClass);
143+
if (!canRead(mediaType)) {
144+
return false;
145+
}
143146
if (!logger.isWarnEnabled()) {
144-
return (this.objectMapper.canDeserialize(javaType) && canRead(mediaType));
147+
return this.objectMapper.canDeserialize(javaType);
145148
}
146149
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
147-
if (this.objectMapper.canDeserialize(javaType, causeRef) && canRead(mediaType)) {
150+
if (this.objectMapper.canDeserialize(javaType, causeRef)) {
148151
return true;
149152
}
150153
Throwable cause = causeRef.get();
@@ -162,11 +165,14 @@ public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
162165

163166
@Override
164167
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
168+
if (!canWrite(mediaType)) {
169+
return false;
170+
}
165171
if (!logger.isWarnEnabled()) {
166-
return (this.objectMapper.canSerialize(clazz) && canWrite(mediaType));
172+
return this.objectMapper.canSerialize(clazz);
167173
}
168174
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
169-
if (this.objectMapper.canSerialize(clazz, causeRef) && canWrite(mediaType)) {
175+
if (this.objectMapper.canSerialize(clazz, causeRef)) {
170176
return true;
171177
}
172178
Throwable cause = causeRef.get();

0 commit comments

Comments
 (0)