DATACMNS-1152 - Setup of ProjectingJackson2HttpMessageConverter now tries to use unique ObjectMapper from ApplicationContext.
We're now trying to look up a uniquely available ObjectMapper instance from the application context falling back to a simple new instance in case none can be found.
This commit is contained in:
@@ -17,8 +17,10 @@ package org.springframework.data.web.config;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -156,7 +158,9 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo
|
||||
if (ClassUtils.isPresent("com.jayway.jsonpath.DocumentContext", context.getClassLoader())
|
||||
&& ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", context.getClassLoader())) {
|
||||
|
||||
ProjectingJackson2HttpMessageConverter converter = new ProjectingJackson2HttpMessageConverter(new ObjectMapper());
|
||||
ObjectMapper mapper = getUniqueBean(ObjectMapper.class, context, () -> new ObjectMapper());
|
||||
|
||||
ProjectingJackson2HttpMessageConverter converter = new ProjectingJackson2HttpMessageConverter(mapper);
|
||||
converter.setBeanFactory(context);
|
||||
forwardBeanClassLoader(converter);
|
||||
|
||||
@@ -193,4 +197,22 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo
|
||||
target.setBeanClassLoader(beanClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the uniquely available bean of the given type from the given {@link ApplicationContext} or the one provided
|
||||
* by the given {@link Supplier} in case the initial lookup fails.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @param context must not be {@literal null}.
|
||||
* @param fallback must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private static <T> T getUniqueBean(Class<T> type, ApplicationContext context, Supplier<T> fallback) {
|
||||
|
||||
try {
|
||||
return context.getBean(type);
|
||||
} catch (NoSuchBeanDefinitionException o_O) {
|
||||
return fallback.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user