Add Smile and CBOR Jackson data formats support
This commit adds Smile and CBOR Jackson HttpMessageConverters and make it possible to create Smile and CBOR ObjectMapper via Jackson2ObjectMapperBuilder, which now allows to specify any custom JsonFactory. Like with JSON and XML Jackson support, the relevant HttpMessageConverters are automaticially configurered by Spring MVC WebMvcConfigurationSupport if jackson-dataformat-smile or jackson-dataformat-cbor dependencies are found in the classpath. Issue: SPR-14435
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.converter.cbor;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
|
||||
* that can read and write <a href="http://cbor.io/">CBOR</a> data format using
|
||||
* <a href="https://github.com/FasterXML/jackson-dataformats-binary/tree/master/cbor">
|
||||
* the dedicated Jackson 2.x extension</a>.
|
||||
*
|
||||
* <p>By default, this converter supports {@code "application/cbor"} media type. This can be
|
||||
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.6 and higher.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MappingJackson2CborHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2CborHttpMessageConverter} using default configuration
|
||||
* provided by {@code Jackson2ObjectMapperBuilder}.
|
||||
*/
|
||||
public MappingJackson2CborHttpMessageConverter() {
|
||||
this(Jackson2ObjectMapperBuilder.cbor().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2CborHttpMessageConverter} with a custom {@link ObjectMapper}
|
||||
* (must be configured with a {@code CBORFactory} instance).
|
||||
* You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
|
||||
* @see Jackson2ObjectMapperBuilder#cbor()
|
||||
*/
|
||||
public MappingJackson2CborHttpMessageConverter(ObjectMapper objectMapper) {
|
||||
super(objectMapper, new MediaType("application", "cbor"));
|
||||
Assert.isAssignable(CBORFactory.class, objectMapper.getFactory().getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* The {@code objectMapper} parameter must be configured with a {@code CBORFactory} instance.
|
||||
*/
|
||||
@Override
|
||||
public void setObjectMapper(ObjectMapper objectMapper) {
|
||||
Assert.isAssignable(CBORFactory.class, objectMapper.getFactory().getClass());
|
||||
super.setObjectMapper(objectMapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,6 +31,7 @@ import javax.xml.stream.XMLResolver;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.AnnotationIntrospector;
|
||||
@@ -47,6 +48,8 @@ import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
|
||||
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
@@ -92,6 +95,8 @@ public class Jackson2ObjectMapperBuilder {
|
||||
|
||||
private boolean createXmlMapper = false;
|
||||
|
||||
private JsonFactory factory;
|
||||
|
||||
private DateFormat dateFormat;
|
||||
|
||||
private Locale locale;
|
||||
@@ -143,6 +148,16 @@ public class Jackson2ObjectMapperBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the {@link JsonFactory} to be used to create the {@link ObjectMapper}
|
||||
* instance.
|
||||
* @since 5.0
|
||||
*/
|
||||
public Jackson2ObjectMapperBuilder factory(JsonFactory factory) {
|
||||
this.factory = factory;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the format for date/time with the given {@link DateFormat}.
|
||||
* <p>Note: Setting this property makes the exposed {@link ObjectMapper}
|
||||
@@ -585,7 +600,7 @@ public class Jackson2ObjectMapperBuilder {
|
||||
new XmlObjectMapperInitializer().create());
|
||||
}
|
||||
else {
|
||||
mapper = new ObjectMapper();
|
||||
mapper = (this.factory != null ? new ObjectMapper(this.factory) : new ObjectMapper());
|
||||
}
|
||||
configure(mapper);
|
||||
return (T) mapper;
|
||||
@@ -794,6 +809,24 @@ public class Jackson2ObjectMapperBuilder {
|
||||
return new Jackson2ObjectMapperBuilder().createXmlMapper(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
|
||||
* build a Smile data format {@link ObjectMapper} instance.
|
||||
* @since 5.0
|
||||
*/
|
||||
public static Jackson2ObjectMapperBuilder smile() {
|
||||
return new Jackson2ObjectMapperBuilder().factory(new SmileFactoryInitializer().create());
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
|
||||
* build a CBOR data format {@link ObjectMapper} instance.
|
||||
* @since 5.0
|
||||
*/
|
||||
public static Jackson2ObjectMapperBuilder cbor() {
|
||||
return new Jackson2ObjectMapperBuilder().factory(new CborFactoryInitializer().create());
|
||||
}
|
||||
|
||||
|
||||
private static class XmlObjectMapperInitializer {
|
||||
|
||||
@@ -823,4 +856,16 @@ public class Jackson2ObjectMapperBuilder {
|
||||
};
|
||||
}
|
||||
|
||||
private static class SmileFactoryInitializer {
|
||||
public JsonFactory create() {
|
||||
return new SmileFactory();
|
||||
}
|
||||
}
|
||||
|
||||
private static class CborFactoryInitializer {
|
||||
public JsonFactory create() {
|
||||
return new CBORFactory();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.TimeZone;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.databind.AnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
@@ -163,6 +164,15 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
|
||||
this.builder.createXmlMapper(createXmlMapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the {@link JsonFactory} to be used to create the {@link ObjectMapper}
|
||||
* instance.
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setFactory(JsonFactory factory) {
|
||||
this.builder.factory(factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the format for date/time with the given {@link DateFormat}.
|
||||
* <p>Note: Setting this property makes the exposed {@link ObjectMapper}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.converter.smile;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
|
||||
* that can read and write Smile data format ("binary JSON") using
|
||||
* <a href="https://github.com/FasterXML/jackson-dataformats-binary/tree/master/smile">
|
||||
* the dedicated Jackson 2.x extension</a>.
|
||||
*
|
||||
* <p>By default, this converter supports {@code "application/x-jackson-smile"} media type.
|
||||
* This can be overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* <p>Compatible with Jackson 2.6 and higher.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MappingJackson2SmileHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2SmileHttpMessageConverter} using default configuration
|
||||
* provided by {@code Jackson2ObjectMapperBuilder}.
|
||||
*/
|
||||
public MappingJackson2SmileHttpMessageConverter() {
|
||||
this(Jackson2ObjectMapperBuilder.smile().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2SmileHttpMessageConverter} with a custom {@link ObjectMapper}
|
||||
* (must be configured with a {@code SmileFactory} instance).
|
||||
* You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
|
||||
* @see Jackson2ObjectMapperBuilder#smile()
|
||||
*/
|
||||
public MappingJackson2SmileHttpMessageConverter(ObjectMapper objectMapper) {
|
||||
super(objectMapper, new MediaType("application", "x-jackson-smile"));
|
||||
Assert.isAssignable(SmileFactory.class, objectMapper.getFactory().getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* The {@code objectMapper} parameter must be configured with a {@code SmileFactory} instance.
|
||||
*/
|
||||
@Override
|
||||
public void setObjectMapper(ObjectMapper objectMapper) {
|
||||
Assert.isAssignable(SmileFactory.class, objectMapper.getFactory().getClass());
|
||||
super.setObjectMapper(objectMapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package org.springframework.http.converter.support;
|
||||
import org.springframework.http.converter.FormHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.GsonHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
|
||||
@@ -44,6 +45,9 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
|
||||
private static final boolean jackson2XmlPresent =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
|
||||
|
||||
private static final boolean jackson2SmilePresent =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
|
||||
|
||||
private static final boolean gsonPresent =
|
||||
ClassUtils.isPresent("com.google.gson.Gson", AllEncompassingFormHttpMessageConverter.class.getClassLoader());
|
||||
|
||||
@@ -65,6 +69,10 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
|
||||
if (jackson2XmlPresent) {
|
||||
addPartConverter(new MappingJackson2XmlHttpMessageConverter());
|
||||
}
|
||||
|
||||
if (jackson2SmilePresent) {
|
||||
addPartConverter(new MappingJackson2SmileHttpMessageConverter());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,10 +40,12 @@ import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.ResourceHttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
|
||||
import org.springframework.http.converter.feed.AtomFeedHttpMessageConverter;
|
||||
import org.springframework.http.converter.feed.RssChannelHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.GsonHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
|
||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
||||
@@ -133,6 +135,12 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
private static final boolean jackson2XmlPresent =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", RestTemplate.class.getClassLoader());
|
||||
|
||||
private static final boolean jackson2SmilePresent =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", RestTemplate.class.getClassLoader());
|
||||
|
||||
private static final boolean jackson2CborPresent =
|
||||
ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory", RestTemplate.class.getClassLoader());
|
||||
|
||||
private static final boolean gsonPresent =
|
||||
ClassUtils.isPresent("com.google.gson.Gson", RestTemplate.class.getClassLoader());
|
||||
|
||||
@@ -175,6 +183,13 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
else if (gsonPresent) {
|
||||
this.messageConverters.add(new GsonHttpMessageConverter());
|
||||
}
|
||||
|
||||
if (jackson2SmilePresent) {
|
||||
this.messageConverters.add(new MappingJackson2SmileHttpMessageConverter());
|
||||
}
|
||||
if (jackson2CborPresent) {
|
||||
this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user