Don't replace MappingJackson2HttpMessageConverter

Guard against Spring Data REST TypeConstrained Jackson converters
replacing the default MappingJackson2HttpMessageConverter.

Fixes gh-1968
pull/1979/head
Phillip Webb 10 years ago
parent bf0629522c
commit 70a1438c6f

@ -51,6 +51,14 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp
*/ */
public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> { public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>> {
private static final List<Class<?>> NON_REPLACING_CONVERTERS;
static {
List<Class<?>> nonReplacingConverters = new ArrayList<Class<?>>();
addClassIfExists(nonReplacingConverters, "org.springframework.hateoas.mvc."
+ "TypeConstrainedMappingJackson2HttpMessageConverter");
NON_REPLACING_CONVERTERS = Collections.unmodifiableList(nonReplacingConverters);
}
private final List<HttpMessageConverter<?>> converters; private final List<HttpMessageConverter<?>> converters;
/** /**
@ -95,8 +103,7 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
Iterator<HttpMessageConverter<?>> iterator = processing.iterator(); Iterator<HttpMessageConverter<?>> iterator = processing.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
HttpMessageConverter<?> candidate = iterator.next(); HttpMessageConverter<?> candidate = iterator.next();
if (ClassUtils.isAssignableValue(defaultConverter.getClass(), if (isReplacement(defaultConverter, candidate)) {
candidate)) {
combined.add(candidate); combined.add(candidate);
iterator.remove(); iterator.remove();
} }
@ -109,6 +116,16 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
this.converters = Collections.unmodifiableList(combined); this.converters = Collections.unmodifiableList(combined);
} }
private boolean isReplacement(HttpMessageConverter<?> defaultConverter,
HttpMessageConverter<?> candidate) {
for (Class<?> nonRelacingConverter : NON_REPLACING_CONVERTERS) {
if (nonRelacingConverter.isInstance(candidate)) {
return false;
}
}
return ClassUtils.isAssignableValue(defaultConverter.getClass(), candidate);
}
/** /**
* Method that can be used to post-process the {@link HttpMessageConverter} list * Method that can be used to post-process the {@link HttpMessageConverter} list
* before it is used. * before it is used.
@ -165,4 +182,13 @@ public class HttpMessageConverters implements Iterable<HttpMessageConverter<?>>
return this.converters; return this.converters;
} }
private static void addClassIfExists(List<Class<?>> list, String className) {
try {
list.add(Class.forName(className));
}
catch (ClassNotFoundException ex) {
// Ignore
}
}
} }

Loading…
Cancel
Save