Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
f4ccaa87
Commit
f4ccaa87
authored
Aug 25, 2014
by
David Liu
Committed by
Andy Wilkinson
Aug 27, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Provide auto-configuration for Gson
Closes #1339
parent
9f37dfcd
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
168 additions
and
1 deletion
+168
-1
pom.xml
spring-boot-autoconfigure/pom.xml
+5
-0
GsonAutoConfiguration.java
...mework/boot/autoconfigure/gson/GsonAutoConfiguration.java
+43
-0
HttpMessageConvertersAutoConfiguration.java
...configure/web/HttpMessageConvertersAutoConfiguration.java
+19
-1
GsonAutoConfigurationTests.java
...k/boot/autoconfigure/gson/GsonAutoConfigurationTests.java
+64
-0
HttpMessageConvertersAutoConfigurationTests.java
...gure/web/HttpMessageConvertersAutoConfigurationTests.java
+31
-0
pom.xml
spring-boot-dependencies/pom.xml
+6
-0
No files found.
spring-boot-autoconfigure/pom.xml
View file @
f4ccaa87
...
...
@@ -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>
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java
0 → 100644
View file @
f4ccaa87
/*
* 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
();
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfiguration.java
View file @
f4ccaa87
...
...
@@ -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
;
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfigurationTests.java
0 → 100644
View file @
f4ccaa87
/*
* 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"
;
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/HttpMessageConvertersAutoConfigurationTests.java
View file @
f4ccaa87
...
...
@@ -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
();
}
}
}
spring-boot-dependencies/pom.xml
View file @
f4ccaa87
...
...
@@ -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>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment