diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java index e2ba765a5ce..ca465a1698d 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java @@ -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 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 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 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 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 diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java index 4da0750a9e9..6c1b0c7913b 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java @@ -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) { - - } - } diff --git a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java index 4bcd2cae7dc..a4fcd106eef 100644 --- a/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java +++ b/spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java @@ -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();