From 0da1eefd74d868cf8728ab318641161595534002 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 27 Feb 2014 17:10:15 -0500 Subject: [PATCH] Add default ContentTypeResolver initialization Previously AbstractMessageConverter did not have a ContentTypeResolver configured by default. However the Java config and XML namespace in spring-messaging and spring-websocket always configured one. This change ensures every AbstractMessageConverter is configured with an instance of DefaultContentTypeResolver by default. This makes sense since all the resolver does is make an attempt to find a content type to use for matching. If it can't it returns null and it's up to the converter to decide whether it can convert or not. Issue: SPR-11462 --- .../converter/AbstractMessageConverter.java | 12 +++++++----- .../MappingJackson2MessageConverterTests.java | 3 +-- .../messaging/converter/MessageConverterTests.java | 6 ------ .../converter/StringMessageConverterTests.java | 3 +-- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java index 74a316e17d..d03d70cdcd 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java @@ -46,7 +46,7 @@ public abstract class AbstractMessageConverter implements MessageConverter { private final List supportedMimeTypes; - private ContentTypeResolver contentTypeResolver; + private ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver(); private boolean strictContentTypeMatch = false; @@ -83,10 +83,12 @@ public abstract class AbstractMessageConverter implements MessageConverter { * Configure the {@link ContentTypeResolver} to use to resolve the content * type of an input message. *

- * By default, no {@code ContentTypeResolver} is configured. When a resolver - * is not configured, then {@link #setStrictContentTypeMatch(boolean)} should - * be left {@code false} (the default) or otherwise this converter will ignore - * all input messages. + * Note that if no resolver is configured, then + * {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should + * be left as {@code false} (the default) or otherwise this converter will + * ignore all messages. + *

+ * By default, a {@code DefaultContentTypeResolver} instance is used. */ public void setContentTypeResolver(ContentTypeResolver resolver) { this.contentTypeResolver = resolver; diff --git a/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java b/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java index 38a52817c2..abcd3dbb04 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,7 +46,6 @@ public class MappingJackson2MessageConverterTests { @Before public void setup() { this.converter = new MappingJackson2MessageConverter(); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); } @Test diff --git a/spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java b/spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java index 6d4ae23483..1f591ae7b5 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java @@ -46,7 +46,6 @@ public class MessageConverterTests { @Before public void setup() { this.converter = new TestMessageConverter(); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); } @Test @@ -81,12 +80,9 @@ public class MessageConverterTests { @Test public void supportsMimeTypeNoneConfigured() { - Message message = MessageBuilder.withPayload( "ABC").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build(); - this.converter = new TestMessageConverter(Collections.emptyList()); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); assertEquals("success-from", this.converter.fromMessage(message, String.class)); } @@ -94,7 +90,6 @@ public class MessageConverterTests { @Test public void canConvertFromStrictContentTypeMatch() { this.converter = new TestMessageConverter(Arrays.asList(MimeTypeUtils.TEXT_PLAIN)); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); this.converter.setStrictContentTypeMatch(true); Message message = MessageBuilder.withPayload("ABC").build(); @@ -110,7 +105,6 @@ public class MessageConverterTests { public void setStrictContentTypeMatchWithNoSupportedMimeTypes() { Message message = MessageBuilder.withPayload("ABC").build(); this.converter = new TestMessageConverter(Collections.emptyList()); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); this.converter.setStrictContentTypeMatch(true); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java b/spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java index 11bcc1e98c..9715b6d31f 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +43,6 @@ public class StringMessageConverterTests { @Before public void setUp() { this.converter = new StringMessageConverter(); - this.converter.setContentTypeResolver(new DefaultContentTypeResolver()); }