Add JSON converters to FormHttpMessageConverter

The FormHttpMessageConverter now adds Jackson JSON converters
(version 1.x or 2.x) if available on the classpath.

Issue: SPR-10055
This commit is contained in:
Rossen Stoyanchev 2012-12-03 15:07:42 -05:00
parent 42a928596f
commit 7ff964afa8
2 changed files with 19 additions and 1 deletions

View File

@ -36,11 +36,15 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
/**
* Implementation of {@link HttpMessageConverter} that can handle form data, including multipart form data (i.e. file
@ -72,6 +76,14 @@ import org.springframework.util.StringUtils;
*/
public class FormHttpMessageConverter implements HttpMessageConverter<MultiValueMap<String, ?>> {
private static final boolean jackson2Present =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", RestTemplate.class.getClassLoader()) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", RestTemplate.class.getClassLoader());
private static final boolean jacksonPresent =
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", RestTemplate.class.getClassLoader()) &&
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", RestTemplate.class.getClassLoader());
private static final byte[] BOUNDARY_CHARS =
new byte[]{'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
@ -96,6 +108,12 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
stringHttpMessageConverter.setWriteAcceptCharset(false);
this.partConverters.add(stringHttpMessageConverter);
this.partConverters.add(new ResourceHttpMessageConverter());
if (jackson2Present) {
this.partConverters.add(new MappingJackson2HttpMessageConverter());
}
else if (jacksonPresent) {
this.partConverters.add(new MappingJacksonHttpMessageConverter());
}
}

View File

@ -102,7 +102,7 @@ Changes in version 3.2 RC1 (2012-11-04)
* use concurrent cache to improve performance of GenericTypeResolver (SPR-8701)
* cache and late resolve annotations on bean properties to improve performance (SPR-9166)
* allow PropertyResolver implementations to ignore unresolvable ${placeholders} (SPR-9569)
* FormHttpMessageConverter now adds Jackson JSON converters if available on the classpath (SPR-10055)
Changes in version 3.2 M2 (2012-09-11)
--------------------------------------