Browse Source

Move customize(Un)Marshaller methods to abstract class

Issue: SPR-11488
pull/466/merge
Rossen Stoyanchev 12 years ago
parent
commit
c4000727ef
  1. 30
      spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java
  2. 24
      spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java
  3. 2
      spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java

30
spring-web/src/main/java/org/springframework/http/converter/xml/AbstractJaxb2HttpMessageConverter.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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 @@ -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 @@ -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

24
spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

@ -89,7 +89,6 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa @@ -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 @@ -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 @@ -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) {
}
}

2
spring-web/src/test/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverterTests.java

@ -154,6 +154,8 @@ public class Jaxb2RootElementHttpMessageConverterTests { @@ -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();

Loading…
Cancel
Save