Commit f4ccaa87 authored by David Liu's avatar David Liu Committed by Andy Wilkinson

Provide auto-configuration for Gson

Closes #1339
parent 9f37dfcd
......@@ -50,6 +50,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
......
/*
* 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();
}
}
......@@ -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;
}
}
}
/*
* 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";
}
}
......@@ -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();
}
}
}
......@@ -75,6 +75,7 @@
<hornetq.version>2.4.3.Final</hornetq.version>
<hsqldb.version>2.3.2</hsqldb.version>
<jackson.version>2.4.2</jackson.version>
<gson.version>2.3</gson.version>
<janino.version>2.6.1</janino.version>
<javassist.version>3.18.1-GA</javassist.version> <!-- Same as Hibernate -->
<javax-cache.version>1.0.0</javax-cache.version>
......@@ -398,6 +399,11 @@
<artifactId>metrics-servlets</artifactId>
<version>${codahale-metrics.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.btm</groupId>
<artifactId>btm</artifactId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment