diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java index 06a8b5c19b..5463679aec 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java @@ -57,19 +57,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter { /** - * Construct a {@code MarshallingMessageConverter} supporting one or more custom MIME - * types. - * @param supportedMimeTypes the supported MIME types - */ - public MarshallingMessageConverter(MimeType... supportedMimeTypes) { - super(Arrays.asList(supportedMimeTypes)); - } - - /** - * Construct a new {@code MarshallingMessageConverter} with no {@link Marshaller} or - * {@link Unmarshaller} set. The Marshaller and Unmarshaller must be set after - * construction by invoking {@link #setMarshaller(Marshaller)} and {@link - * #setUnmarshaller(Unmarshaller)} . + * Default construct allowing for {@link #setMarshaller(Marshaller)} and/or + * {@link #setUnmarshaller(Unmarshaller)} to be invoked separately. */ public MarshallingMessageConverter() { this(new MimeType("application", "xml"), new MimeType("text", "xml"), @@ -77,15 +66,20 @@ public class MarshallingMessageConverter extends AbstractMessageConverter { } /** - * Construct a new {@code MarshallingMessageConverter} with the given {@link - * Marshaller} set. + * Constructor with a given list of MIME types to support. + * @param supportedMimeTypes the MIME types + */ + public MarshallingMessageConverter(MimeType... supportedMimeTypes) { + super(Arrays.asList(supportedMimeTypes)); + } + + /** + * Constructor with {@link Marshaller}. If the given {@link Marshaller} also + * implements {@link Unmarshaller}, it is also used for unmarshalling. * - *
If the given {@link Marshaller} also implements the {@link Unmarshaller} - * interface, it is used for both marshalling and unmarshalling. Otherwise, an - * exception is thrown. + *
Note that all {@code Marshaller} implementations in Spring also implement + * {@code Unmarshaller} so that you can safely use this constructor. * - *
Note that all {@code Marshaller} implementations in Spring also implement the
- * {@code Unmarshaller} interface, so that you can safely use this constructor.
* @param marshaller object used as marshaller and unmarshaller
*/
public MarshallingMessageConverter(Marshaller marshaller) {
@@ -97,20 +91,6 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
}
}
- /**
- * Construct a new {@code MarshallingMessageConverter} with the given {@code
- * Marshaller} and {@code Unmarshaller}.
- * @param marshaller the Marshaller to use
- * @param unmarshaller the Unmarshaller to use
- */
- public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
- this();
- Assert.notNull(marshaller, "Marshaller must not be null");
- Assert.notNull(unmarshaller, "Unmarshaller must not be null");
- this.marshaller = marshaller;
- this.unmarshaller = unmarshaller;
- }
-
/**
* Set the {@link Marshaller} to be used by this message converter.
@@ -119,6 +99,13 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
this.marshaller = marshaller;
}
+ /**
+ * Return the configured Marshaller.
+ */
+ public Marshaller getMarshaller() {
+ return this.marshaller;
+ }
+
/**
* Set the {@link Unmarshaller} to be used by this message converter.
*/
@@ -126,16 +113,24 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
this.unmarshaller = unmarshaller;
}
+ /**
+ * Return the configured unmarshaller.
+ */
+ public Unmarshaller getUnmarshaller() {
+ return this.unmarshaller;
+ }
+
+
@Override
protected boolean canConvertFrom(Message> message, Class> targetClass) {
- return supportsMimeType(message.getHeaders()) && (this.unmarshaller != null) &&
- this.unmarshaller.supports(targetClass);
+ return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null &&
+ this.unmarshaller.supports(targetClass));
}
@Override
protected boolean canConvertTo(Object payload, MessageHeaders headers) {
- return supportsMimeType(headers) && (this.marshaller != null) &&
- this.marshaller.supports(payload.getClass());
+ return (supportsMimeType(headers) && this.marshaller != null &&
+ this.marshaller.supports(payload.getClass()));
}
@Override
@@ -157,12 +152,10 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
return result;
}
catch (UnmarshallingFailureException ex) {
- throw new MessageConversionException(message,
- "Could not unmarshal XML: " + ex.getMessage(), ex);
+ throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
}
catch (IOException ex) {
- throw new MessageConversionException(message,
- "Could not unmarshal XML: " + ex.getMessage(), ex);
+ throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
}
}
@@ -197,12 +190,10 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
}
}
catch (MarshallingFailureException ex) {
- throw new MessageConversionException(
- "Could not marshal XML: " + ex.getMessage(), ex);
+ throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
}
catch (IOException ex) {
- throw new MessageConversionException(
- "Could not marshal XML: " + ex.getMessage(), ex);
+ throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
}
return payload;
}
diff --git a/spring-messaging/src/test/java/org/springframework/messaging/converter/MarshallingMessageConverterTests.java b/spring-messaging/src/test/java/org/springframework/messaging/converter/MarshallingMessageConverterTests.java
index 083def0a8f..e4cca28dbb 100644
--- a/spring-messaging/src/test/java/org/springframework/messaging/converter/MarshallingMessageConverterTests.java
+++ b/spring-messaging/src/test/java/org/springframework/messaging/converter/MarshallingMessageConverterTests.java
@@ -28,7 +28,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
@@ -47,14 +47,16 @@ public class MarshallingMessageConverterTests {
marshaller.setClassesToBeBound(MyBean.class);
marshaller.afterPropertiesSet();
- converter = new MarshallingMessageConverter(marshaller);
+ this.converter = new MarshallingMessageConverter(marshaller);
}
+
@Test
public void fromMessage() throws Exception {
String payload = "