From f4ccaa874ab95973f16ab14df2d23b61bd48d5a4 Mon Sep 17 00:00:00 2001 From: David Liu Date: Mon, 25 Aug 2014 19:45:50 +0800 Subject: [PATCH] Provide auto-configuration for Gson Closes #1339 --- spring-boot-autoconfigure/pom.xml | 5 ++ .../gson/GsonAutoConfiguration.java | 43 +++++++++++++ ...ttpMessageConvertersAutoConfiguration.java | 20 +++++- .../gson/GsonAutoConfigurationTests.java | 64 +++++++++++++++++++ ...ssageConvertersAutoConfigurationTests.java | 31 +++++++++ spring-boot-dependencies/pom.xml | 6 ++ 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index b36152d257..02b7909700 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -50,6 +50,11 @@ jackson-datatype-jsr310 true + + com.google.code.gson + gson + true + org.flywaydb flyway-core diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java new file mode 100644 index 0000000000..4591d4c433 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012-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. + * 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.boot.autoconfigure.gson; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.google.gson.Gson; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Gson. + * + * @author David Liu + * @since 1.2.0 + */ +@Configuration +@ConditionalOnClass(Gson.class) +public class GsonAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public Gson gson() { + return new Gson(); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java index a065abb8f5..3f149066a6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java @@ -23,15 +23,18 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; /** * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s. @@ -40,10 +43,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; * @author Christian Dupuis * @author Piotr Maj * @author Oliver Gierke + * @author David Liu */ @Configuration @ConditionalOnClass(HttpMessageConverter.class) -@Import(JacksonAutoConfiguration.class) +@Import({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class }) public class HttpMessageConvertersAutoConfiguration { @Autowired(required = false) @@ -75,4 +79,18 @@ public class HttpMessageConvertersAutoConfiguration { } + @Configuration + @ConditionalOnClass(Gson.class) + protected static class GsonConfiguration { + + @Bean + @ConditionalOnMissingBean + public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) { + GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); + converter.setGson(gson); + return converter; + } + + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java new file mode 100644 index 0000000000..30eeb0b1f2 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-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. + * 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.boot.autoconfigure.gson; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.google.gson.Gson; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link GsonAutoConfiguration}. + * + * @author David Liu + */ +public class GsonAutoConfigurationTests { + + AnnotationConfigApplicationContext context; + + @Before + public void setUp() { + this.context = new AnnotationConfigApplicationContext(); + } + + @After + public void tearDown() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void gsonRegistration() { + this.context.register(GsonAutoConfiguration.class); + this.context.refresh(); + Gson gson = this.context.getBean(Gson.class); + assertEquals("{\"data\":\"hello\"}", gson.toJson(new DataObject())); + } + + public class DataObject { + + @SuppressWarnings("unused") + private String data = "hello"; + + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java index 57bece1532..eb90051043 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java @@ -21,9 +21,11 @@ import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -33,6 +35,7 @@ import static org.junit.Assert.assertTrue; * * @author Dave Syer * @author Oliver Gierke + * @author David Liu */ public class HttpMessageConvertersAutoConfigurationTests { @@ -76,4 +79,32 @@ public class HttpMessageConvertersAutoConfigurationTests { } + @Test + public void customGsonConverter() throws Exception { + this.context.register(GsonConfig.class, + HttpMessageConvertersAutoConfiguration.class); + this.context.refresh(); + GsonHttpMessageConverter converter = this.context + .getBean(GsonHttpMessageConverter.class); + assertEquals(this.context.getBean(Gson.class), converter.getGson()); + HttpMessageConverters converters = this.context + .getBean(HttpMessageConverters.class); + assertTrue(converters.getConverters().contains(converter)); + } + + @Configuration + protected static class GsonConfig { + + @Bean + public GsonHttpMessageConverter gsonMessageConverter() { + GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); + converter.setGson(gson()); + return converter; + } + + @Bean + public Gson gson() { + return new Gson(); + } + } } diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 255fb6da2f..18be92c8c1 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -75,6 +75,7 @@ 2.4.3.Final 2.3.2 2.4.2 + 2.3 2.6.1 3.18.1-GA 1.0.0 @@ -398,6 +399,11 @@ metrics-servlets ${codahale-metrics.version} + + com.google.code.gson + gson + ${gson.version} + org.codehaus.btm btm