Browse Source

Support Jackson based XML serialization in RestTemplate

Issue: SPR-12225
pull/650/head
Sebastien Deleuze 10 years ago
parent
commit
2989fe4203
  1. 9
      spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
  2. 2
      spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

9
spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

@ -47,6 +47,7 @@ import org.springframework.http.converter.json.GsonHttpMessageConverter; @@ -47,6 +47,7 @@ import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@ -122,6 +123,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat @@ -122,6 +123,9 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", RestTemplate.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", RestTemplate.class.getClassLoader());
private static final boolean jackson2XmlPresent =
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", RestTemplate.class.getClassLoader());
private static final boolean gsonPresent =
ClassUtils.isPresent("com.google.gson.Gson", RestTemplate.class.getClassLoader());
@ -148,7 +152,10 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat @@ -148,7 +152,10 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
this.messageConverters.add(new AtomFeedHttpMessageConverter());
this.messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
if (jackson2XmlPresent) {
messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
}
else if (jaxb2Present) {
this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {

2
spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

@ -222,7 +222,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase { @@ -222,7 +222,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
MySampleBean bean = new MySampleBean("with", "with", "without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jacksonValue);
HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jacksonValue, entityHeaders);
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
assertTrue(s.contains("\"with1\":\"with\""));
assertFalse(s.contains("\"with2\":\"with\""));

Loading…
Cancel
Save