Use Jackson improved default configuration everywhere
With this commit, Jackson builder is now used in spring-websocket to create the ObjectMapper instance. It is not possible to use the builder for spring-messaging and spring-jms since these modules don't have a dependency on spring-web, thus they now just customize the same features: - MapperFeature#DEFAULT_VIEW_INCLUSION is disabled - DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES is disabled Issue: SPR-12293
This commit is contained in:
@@ -26,7 +26,9 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import com.fasterxml.jackson.core.JsonEncoding;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
|
||||
@@ -39,6 +41,12 @@ import org.springframework.util.MimeType;
|
||||
/**
|
||||
* A Jackson 2 based {@link MessageConverter} implementation.
|
||||
*
|
||||
* <p>It customizes Jackson's default properties with the following ones:
|
||||
* <ul>
|
||||
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
|
||||
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Compatible with Jackson 2.1 and higher.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
@@ -52,16 +60,18 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
|
||||
ClassUtils.hasMethod(ObjectMapper.class, "canDeserialize", JavaType.class, AtomicReference.class);
|
||||
|
||||
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private Boolean prettyPrint;
|
||||
|
||||
|
||||
public MappingJackson2MessageConverter() {
|
||||
super(new MimeType("application", "json", Charset.forName("UTF-8")));
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
|
||||
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@code ObjectMapper} for this converter.
|
||||
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used.
|
||||
|
||||
@@ -289,16 +289,20 @@ public abstract class AbstractMessageBrokerConfiguration implements ApplicationC
|
||||
converters.add(new StringMessageConverter());
|
||||
converters.add(new ByteArrayMessageConverter());
|
||||
if (jackson2Present) {
|
||||
DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
|
||||
resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
|
||||
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
|
||||
converter.setContentTypeResolver(resolver);
|
||||
converters.add(converter);
|
||||
converters.add(createJacksonConverter());
|
||||
}
|
||||
}
|
||||
return new CompositeMessageConverter(converters);
|
||||
}
|
||||
|
||||
protected MappingJackson2MessageConverter createJacksonConverter() {
|
||||
DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
|
||||
resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
|
||||
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
|
||||
converter.setContentTypeResolver(resolver);
|
||||
return converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to add custom message converters.
|
||||
* @param messageConverters the list to add converters to, initially empty
|
||||
|
||||
Reference in New Issue
Block a user