Move customize(Un)Marshaller methods to abstract class

Issue: SPR-11488
This commit is contained in:
Rossen Stoyanchev 2014-02-28 11:54:34 -05:00
parent 45be8c0692
commit c4000727ef
3 changed files with 29 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@ -47,7 +47,9 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt
protected final Marshaller createMarshaller(Class<?> clazz) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
return jaxbContext.createMarshaller();
Marshaller marshaller = jaxbContext.createMarshaller();
customizeMarshaller(marshaller);
return marshaller;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException(
@ -55,6 +57,16 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt
}
}
/**
* Customize the {@link Marshaller} created by this
* message converter before using it to write the object to the output.
* @param marshaller the marshaller to customize
* @see #createMarshaller(Class)
* @since 4.0.3
*/
protected void customizeMarshaller(Marshaller marshaller) {
}
/**
* Create a new {@link Unmarshaller} for the given class.
* @param clazz the class to create the unmarshaller for
@ -64,7 +76,9 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt
protected final Unmarshaller createUnmarshaller(Class<?> clazz) throws JAXBException {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
return jaxbContext.createUnmarshaller();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
customizeUnmarshaller(unmarshaller);
return unmarshaller;
}
catch (JAXBException ex) {
throw new HttpMessageConversionException(
@ -72,6 +86,16 @@ public abstract class AbstractJaxb2HttpMessageConverter<T> extends AbstractXmlHt
}
}
/**
* Customize the {@link Unmarshaller} created by this
* message converter before using it to read the object from the input.
* @param unmarshaller the unmarshaller to customize
* @see #createUnmarshaller(Class)
* @since 4.0.3
*/
protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
}
/**
* Return a {@link JAXBContext} for the given class.
* @param clazz the class to return the context for

View File

@ -89,7 +89,6 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
try {
source = processSource(source);
Unmarshaller unmarshaller = createUnmarshaller(clazz);
this.customizeUnmarshaller(unmarshaller);
if (clazz.isAnnotationPresent(XmlRootElement.class)) {
return unmarshaller.unmarshal(source);
}
@ -132,7 +131,6 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
try {
Class<?> clazz = ClassUtils.getUserClass(o);
Marshaller marshaller = createMarshaller(clazz);
this.customizeMarshaller(marshaller);
setCharset(headers.getContentType(), marshaller);
marshaller.marshal(o, result);
}
@ -150,26 +148,4 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
}
}
/**
* Customize the {@link Marshaller} created by this
* message converter before using it to write the object to the output.
* @param marshaller the marshaller to customize
* @see #createMarshaller(Class)
* @since 4.0.3
*/
protected void customizeMarshaller(Marshaller marshaller) {
}
/**
* Customize the {@link Unmarshaller} created by this
* message converter before using it to read the object from the input.
* @param unmarshaller the unmarshaller to customize
* @see #createUnmarshaller(Class)
* @since 4.0.3
*/
protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
}
}

View File

@ -154,6 +154,8 @@ public class Jaxb2RootElementHttpMessageConverterTests {
outputMessage.getBodyAsString(Charset.forName("UTF-8")));
}
// SPR-11488
@Test
public void customizeMarshaller() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();