This commit is contained in:
Rossen Stoyanchev 2015-05-07 12:00:55 -04:00
parent a36319e91c
commit eb9eadbb50
2 changed files with 46 additions and 52 deletions

View File

@ -57,19 +57,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
/** /**
* Construct a {@code MarshallingMessageConverter} supporting one or more custom MIME * Default construct allowing for {@link #setMarshaller(Marshaller)} and/or
* types. * {@link #setUnmarshaller(Unmarshaller)} to be invoked separately.
* @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)} .
*/ */
public MarshallingMessageConverter() { public MarshallingMessageConverter() {
this(new MimeType("application", "xml"), new MimeType("text", "xml"), 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 * Constructor with a given list of MIME types to support.
* Marshaller} set. * @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.
* *
* <p>If the given {@link Marshaller} also implements the {@link Unmarshaller} * <p>Note that all {@code Marshaller} implementations in Spring also implement
* interface, it is used for both marshalling and unmarshalling. Otherwise, an * {@code Unmarshaller} so that you can safely use this constructor.
* exception is thrown.
* *
* <p>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 * @param marshaller object used as marshaller and unmarshaller
*/ */
public MarshallingMessageConverter(Marshaller marshaller) { 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. * Set the {@link Marshaller} to be used by this message converter.
@ -119,6 +99,13 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
this.marshaller = marshaller; this.marshaller = marshaller;
} }
/**
* Return the configured Marshaller.
*/
public Marshaller getMarshaller() {
return this.marshaller;
}
/** /**
* Set the {@link Unmarshaller} to be used by this message converter. * Set the {@link Unmarshaller} to be used by this message converter.
*/ */
@ -126,16 +113,24 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
this.unmarshaller = unmarshaller; this.unmarshaller = unmarshaller;
} }
/**
* Return the configured unmarshaller.
*/
public Unmarshaller getUnmarshaller() {
return this.unmarshaller;
}
@Override @Override
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) { protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
return supportsMimeType(message.getHeaders()) && (this.unmarshaller != null) && return (supportsMimeType(message.getHeaders()) && this.unmarshaller != null &&
this.unmarshaller.supports(targetClass); this.unmarshaller.supports(targetClass));
} }
@Override @Override
protected boolean canConvertTo(Object payload, MessageHeaders headers) { protected boolean canConvertTo(Object payload, MessageHeaders headers) {
return supportsMimeType(headers) && (this.marshaller != null) && return (supportsMimeType(headers) && this.marshaller != null &&
this.marshaller.supports(payload.getClass()); this.marshaller.supports(payload.getClass()));
} }
@Override @Override
@ -157,12 +152,10 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
return result; return result;
} }
catch (UnmarshallingFailureException ex) { catch (UnmarshallingFailureException ex) {
throw new MessageConversionException(message, throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
"Could not unmarshal XML: " + ex.getMessage(), ex);
} }
catch (IOException ex) { catch (IOException ex) {
throw new MessageConversionException(message, throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
"Could not unmarshal XML: " + ex.getMessage(), ex);
} }
} }
@ -197,12 +190,10 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
} }
} }
catch (MarshallingFailureException ex) { catch (MarshallingFailureException ex) {
throw new MessageConversionException( throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
"Could not marshal XML: " + ex.getMessage(), ex);
} }
catch (IOException ex) { catch (IOException ex) {
throw new MessageConversionException( throw new MessageConversionException("Could not marshal XML: " + ex.getMessage(), ex);
"Could not marshal XML: " + ex.getMessage(), ex);
} }
return payload; return payload;
} }

View File

@ -28,7 +28,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.oxm.jaxb.Jaxb2Marshaller; import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -47,14 +47,16 @@ public class MarshallingMessageConverterTests {
marshaller.setClassesToBeBound(MyBean.class); marshaller.setClassesToBeBound(MyBean.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
converter = new MarshallingMessageConverter(marshaller); this.converter = new MarshallingMessageConverter(marshaller);
} }
@Test @Test
public void fromMessage() throws Exception { public void fromMessage() throws Exception {
String payload = "<myBean><name>Foo</name></myBean>"; String payload = "<myBean><name>Foo</name></myBean>";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build(); Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
MyBean actual = (MyBean) converter.fromMessage(message, MyBean.class); MyBean actual = (MyBean) this.converter.fromMessage(message, MyBean.class);
assertNotNull(actual);
assertEquals("Foo", actual.getName()); assertEquals("Foo", actual.getName());
} }
@ -62,14 +64,14 @@ public class MarshallingMessageConverterTests {
public void fromMessageInvalidXml() throws Exception { public void fromMessageInvalidXml() throws Exception {
String payload = "<myBean><name>Foo</name><myBean>"; String payload = "<myBean><name>Foo</name><myBean>";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build(); Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
converter.fromMessage(message, MyBean.class); this.converter.fromMessage(message, MyBean.class);
} }
@Test(expected = MessageConversionException.class) @Test(expected = MessageConversionException.class)
public void fromMessageValidXmlWithUnknownProperty() throws IOException { public void fromMessageValidXmlWithUnknownProperty() throws IOException {
String payload = "<myBean><age>42</age><myBean>"; String payload = "<myBean><age>42</age><myBean>";
Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build(); Message<?> message = MessageBuilder.withPayload(payload.getBytes(UTF_8)).build();
MyBean myBean = (MyBean)converter.fromMessage(message, MyBean.class); this.converter.fromMessage(message, MyBean.class);
} }
@Test @Test
@ -77,7 +79,8 @@ public class MarshallingMessageConverterTests {
MyBean payload = new MyBean(); MyBean payload = new MyBean();
payload.setName("Foo"); payload.setName("Foo");
Message<?> message = converter.toMessage(payload, null); Message<?> message = this.converter.toMessage(payload, null);
assertNotNull(message);
String actual = new String((byte[]) message.getPayload(), UTF_8); String actual = new String((byte[]) message.getPayload(), UTF_8);
assertXMLEqual("<myBean><name>Foo</name></myBean>", actual); assertXMLEqual("<myBean><name>Foo</name></myBean>", actual);