Skip to content

Commit

Permalink
Move feed message converters ahead of jackson/jaxb2
Browse files Browse the repository at this point in the history
The Atom/RSS message converters are now registered ahead of the
Jackson and the JAXB2 message converters by default. Since the Atom
and RSS converters convert to and from very specific object types
Feed and Channel respectively, that shouldn't introduce any regressions
and will work more intuitively when the requested media type is "*/*".

Issue: SPR-9054
  • Loading branch information
rstoyanchev committed Jul 10, 2012
1 parent 2b4a516 commit e860fa9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public RestTemplate() {
this.messageConverters.add(new ResourceHttpMessageConverter());
this.messageConverters.add(new SourceHttpMessageConverter());
this.messageConverters.add(new XmlAwareFormHttpMessageConverter());
if (romePresent) {
this.messageConverters.add(new AtomFeedHttpMessageConverter());
this.messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
Expand All @@ -158,10 +162,6 @@ public RestTemplate() {
else if (jacksonPresent) {
this.messageConverters.add(new MappingJacksonHttpMessageConverter());
}
if (romePresent) {
this.messageConverters.add(new AtomFeedHttpMessageConverter());
this.messageConverters.add(new RssChannelHttpMessageConverter());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,17 @@ private ManagedList<?> getMessageConverters(Element element, Object source, Pars
messageConverters.setSource(source);
messageConverters.add(createConverterBeanDefinition(ByteArrayHttpMessageConverter.class, source));

RootBeanDefinition stringConverterDef = createConverterBeanDefinition(StringHttpMessageConverter.class,
source);
RootBeanDefinition stringConverterDef = createConverterBeanDefinition(StringHttpMessageConverter.class, source);
stringConverterDef.getPropertyValues().add("writeAcceptCharset", false);
messageConverters.add(stringConverterDef);

messageConverters.add(createConverterBeanDefinition(ResourceHttpMessageConverter.class, source));
messageConverters.add(createConverterBeanDefinition(SourceHttpMessageConverter.class, source));
messageConverters.add(createConverterBeanDefinition(XmlAwareFormHttpMessageConverter.class, source));
if (romePresent) {
messageConverters.add(createConverterBeanDefinition(AtomFeedHttpMessageConverter.class, source));
messageConverters.add(createConverterBeanDefinition(RssChannelHttpMessageConverter.class, source));
}
if (jaxb2Present) {
messageConverters
.add(createConverterBeanDefinition(Jaxb2RootElementHttpMessageConverter.class, source));
Expand All @@ -320,16 +323,13 @@ private ManagedList<?> getMessageConverters(Element element, Object source, Pars
else if (jacksonPresent) {
messageConverters.add(createConverterBeanDefinition(MappingJacksonHttpMessageConverter.class, source));
}
if (romePresent) {
messageConverters.add(createConverterBeanDefinition(AtomFeedHttpMessageConverter.class, source));
messageConverters.add(createConverterBeanDefinition(RssChannelHttpMessageConverter.class, source));
}
}
return messageConverters;
}

private RootBeanDefinition createConverterBeanDefinition(Class<? extends HttpMessageConverter> converterClass,
Object source) {
private RootBeanDefinition createConverterBeanDefinition(
Class<? extends HttpMessageConverter> converterClass, Object source) {

RootBeanDefinition beanDefinition = new RootBeanDefinition(converterClass);
beanDefinition.setSource(source);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@
*/
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {

private static final boolean jaxb2Present =
ClassUtils.isPresent("javax.xml.bind.Binder", WebMvcConfigurationSupport.class.getClassLoader());

private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", WebMvcConfigurationSupport.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader());

private static final boolean jacksonPresent =
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", WebMvcConfigurationSupport.class.getClassLoader()) &&
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader());

private static boolean romePresent =
ClassUtils.isPresent("com.sun.syndication.feed.WireFeed", WebMvcConfigurationSupport.class.getClassLoader());

private ServletContext servletContext;

private ApplicationContext applicationContext;
Expand Down Expand Up @@ -435,21 +449,19 @@ protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new XmlAwareFormHttpMessageConverter());

ClassLoader classLoader = getClass().getClassLoader();
if (ClassUtils.isPresent("javax.xml.bind.Binder", classLoader)) {
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader)) {
if (jackson2Present) {
messageConverters.add(new MappingJackson2HttpMessageConverter());
}
else if (ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", classLoader)) {
else if (jacksonPresent) {
messageConverters.add(new MappingJacksonHttpMessageConverter());
}
if (ClassUtils.isPresent("com.sun.syndication.feed.WireFeed", classLoader)) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/dist/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Changes in version 3.2 M2 (2012-08-xx)
* DispatcherPortlet does not forward event exceptions to the render phase by default (SPR-9287)
* add defaultCharset property to StringHttpMessageConverter
* add @ExceptionResolver annotation to detect classes with @ExceptionHandler methods
* move RSS/Atom message converter registration ahead of jackson/jaxb2


Changes in version 3.2 M1 (2012-05-28)
Expand Down

0 comments on commit e860fa9

Please sign in to comment.