Skip to content

Commit 334b4a9

Browse files
committed
Jackson-based message converters consistently check media type first
Issue: SPR-14163
1 parent 416a24c commit 334b4a9

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java

+11-8
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-2016 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.
@@ -142,20 +142,20 @@ private void configurePrettyPrint() {
142142

143143
@Override
144144
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
145-
if (targetClass == null) {
145+
if (targetClass == null || !supportsMimeType(message.getHeaders())) {
146146
return false;
147147
}
148148
JavaType javaType = this.objectMapper.constructType(targetClass);
149149
if (!jackson23Available || !logger.isWarnEnabled()) {
150-
return (this.objectMapper.canDeserialize(javaType) && supportsMimeType(message.getHeaders()));
150+
return this.objectMapper.canDeserialize(javaType);
151151
}
152152
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
153-
if (this.objectMapper.canDeserialize(javaType, causeRef) && supportsMimeType(message.getHeaders())) {
153+
if (this.objectMapper.canDeserialize(javaType, causeRef)) {
154154
return true;
155155
}
156156
Throwable cause = causeRef.get();
157157
if (cause != null) {
158-
String msg = "Failed to evaluate deserialization for type " + javaType;
158+
String msg = "Failed to evaluate Jackson deserialization for type " + javaType;
159159
if (logger.isDebugEnabled()) {
160160
logger.warn(msg, cause);
161161
}
@@ -168,16 +168,19 @@ protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
168168

169169
@Override
170170
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
171+
if (payload == null || !supportsMimeType(headers)) {
172+
return false;
173+
}
171174
if (!jackson23Available || !logger.isWarnEnabled()) {
172-
return (this.objectMapper.canSerialize(payload.getClass()) && supportsMimeType(headers));
175+
return this.objectMapper.canSerialize(payload.getClass());
173176
}
174177
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
175-
if (this.objectMapper.canSerialize(payload.getClass(), causeRef) && supportsMimeType(headers)) {
178+
if (this.objectMapper.canSerialize(payload.getClass(), causeRef)) {
176179
return true;
177180
}
178181
Throwable cause = causeRef.get();
179182
if (cause != null) {
180-
String msg = "Failed to evaluate serialization for type [" + payload.getClass() + "]";
183+
String msg = "Failed to evaluate Jackson serialization for type [" + payload.getClass() + "]";
181184
if (logger.isDebugEnabled()) {
182185
logger.warn(msg, cause);
183186
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
142142

143143
@Override
144144
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
145-
JavaType javaType = getJavaType(type, contextClass);
146145
if (!canRead(mediaType)) {
147146
return false;
148147
}
148+
JavaType javaType = getJavaType(type, contextClass);
149149
if (!jackson23Available || !logger.isWarnEnabled()) {
150150
return this.objectMapper.canDeserialize(javaType);
151151
}
@@ -155,7 +155,7 @@ public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
155155
}
156156
Throwable cause = causeRef.get();
157157
if (cause != null) {
158-
String msg = "Failed to evaluate deserialization for type " + javaType;
158+
String msg = "Failed to evaluate Jackson deserialization for type " + javaType;
159159
if (logger.isDebugEnabled()) {
160160
logger.warn(msg, cause);
161161
}
@@ -180,7 +180,7 @@ public boolean canWrite(Class<?> clazz, MediaType mediaType) {
180180
}
181181
Throwable cause = causeRef.get();
182182
if (cause != null) {
183-
String msg = "Failed to evaluate serialization for type [" + clazz + "]";
183+
String msg = "Failed to evaluate Jackson serialization for type [" + clazz + "]";
184184
if (logger.isDebugEnabled()) {
185185
logger.warn(msg, cause);
186186
}

spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java

+7-5
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-2016 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.
@@ -31,7 +31,6 @@
3131
import com.fasterxml.jackson.databind.ser.FilterProvider;
3232
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
3333
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
34-
3534
import org.junit.Test;
3635

3736
import org.springframework.core.ParameterizedTypeReference;
@@ -68,9 +67,7 @@ public void canWrite() {
6867
assertTrue(converter.canWrite(Map.class, new MediaType("application", "json")));
6968
}
7069

71-
// SPR-7905
72-
73-
@Test
70+
@Test // SPR-7905
7471
public void canReadAndWriteMicroformats() {
7572
assertTrue(converter.canRead(MyBean.class, new MediaType("application", "vnd.test-micro-type+json")));
7673
assertTrue(converter.canWrite(MyBean.class, new MediaType("application", "vnd.test-micro-type+json")));
@@ -439,9 +436,12 @@ public void setName(String name) {
439436
}
440437
}
441438

439+
442440
private interface MyJacksonView1 {};
441+
443442
private interface MyJacksonView2 {};
444443

444+
445445
@SuppressWarnings("unused")
446446
private static class JacksonViewBean {
447447

@@ -478,11 +478,13 @@ public void setWithoutView(String withoutView) {
478478
}
479479
}
480480

481+
481482
@JsonFilter("myJacksonFilter")
482483
@SuppressWarnings("unused")
483484
private static class JacksonFilteredBean {
484485

485486
private String property1;
487+
486488
private String property2;
487489

488490
public String getProperty1() {

0 commit comments

Comments
 (0)