Browse Source

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
pull/485/head
Rossen Stoyanchev 11 years ago
parent
commit
0da1eefd74
  1. 12
      spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java
  2. 3
      spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java
  3. 6
      spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java
  4. 3
      spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java

12
spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java

@ -46,7 +46,7 @@ public abstract class AbstractMessageConverter implements MessageConverter { @@ -46,7 +46,7 @@ public abstract class AbstractMessageConverter implements MessageConverter {
private final List<MimeType> supportedMimeTypes;
private ContentTypeResolver contentTypeResolver;
private ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
private boolean strictContentTypeMatch = false;
@ -83,10 +83,12 @@ public abstract class AbstractMessageConverter implements MessageConverter { @@ -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.
* <p>
* 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.
* <p>
* By default, a {@code DefaultContentTypeResolver} instance is used.
*/
public void setContentTypeResolver(ContentTypeResolver resolver) {
this.contentTypeResolver = resolver;

3
spring-messaging/src/test/java/org/springframework/messaging/converter/MappingJackson2MessageConverterTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -46,7 +46,6 @@ public class MappingJackson2MessageConverterTests {
@Before
public void setup() {
this.converter = new MappingJackson2MessageConverter();
this.converter.setContentTypeResolver(new DefaultContentTypeResolver());
}
@Test

6
spring-messaging/src/test/java/org/springframework/messaging/converter/MessageConverterTests.java

@ -46,7 +46,6 @@ public class MessageConverterTests { @@ -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 { @@ -81,12 +80,9 @@ public class MessageConverterTests {
@Test
public void supportsMimeTypeNoneConfigured() {
Message<String> message = MessageBuilder.withPayload(
"ABC").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build();
this.converter = new TestMessageConverter(Collections.<MimeType>emptyList());
this.converter.setContentTypeResolver(new DefaultContentTypeResolver());
assertEquals("success-from", this.converter.fromMessage(message, String.class));
}
@ -94,7 +90,6 @@ public class MessageConverterTests { @@ -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<String> message = MessageBuilder.withPayload("ABC").build();
@ -110,7 +105,6 @@ public class MessageConverterTests { @@ -110,7 +105,6 @@ public class MessageConverterTests {
public void setStrictContentTypeMatchWithNoSupportedMimeTypes() {
Message<String> message = MessageBuilder.withPayload("ABC").build();
this.converter = new TestMessageConverter(Collections.<MimeType>emptyList());
this.converter.setContentTypeResolver(new DefaultContentTypeResolver());
this.converter.setStrictContentTypeMatch(true);
}

3
spring-messaging/src/test/java/org/springframework/messaging/converter/StringMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -43,7 +43,6 @@ public class StringMessageConverterTests {
@Before
public void setUp() {
this.converter = new StringMessageConverter();
this.converter.setContentTypeResolver(new DefaultContentTypeResolver());
}

Loading…
Cancel
Save