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
261b3afc
Commit
261b3afc
authored
Oct 15, 2014
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply spring.jackson.* config to Spring Data REST object mappers
Closes gh-1698
parent
886bbc3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
RepositoryRestMvcAutoConfiguration.java
...nfigure/data/rest/RepositoryRestMvcAutoConfiguration.java
+14
-0
RepositoryRestMvcAutoConfigurationTests.java
...re/data/rest/RepositoryRestMvcAutoConfigurationTests.java
+37
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java
View file @
261b3afc
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
data
.
rest
;
package
org
.
springframework
.
boot
.
autoconfigure
.
data
.
rest
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
...
@@ -25,6 +26,9 @@ import org.springframework.context.annotation.Bean;
...
@@ -25,6 +26,9 @@ import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.rest.core.config.RepositoryRestConfiguration
;
import
org.springframework.data.rest.core.config.RepositoryRestConfiguration
;
import
org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
;
import
org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
;
import
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
/**
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data Rest's MVC
...
@@ -50,6 +54,9 @@ public class RepositoryRestMvcAutoConfiguration {
...
@@ -50,6 +54,9 @@ public class RepositoryRestMvcAutoConfiguration {
static
class
RepositoryRestMvcBootConfiguration
extends
static
class
RepositoryRestMvcBootConfiguration
extends
RepositoryRestMvcConfiguration
{
RepositoryRestMvcConfiguration
{
@Autowired
(
required
=
false
)
private
Jackson2ObjectMapperBuilder
objectMapperBuilder
;
@Bean
@Bean
@ConfigurationProperties
(
prefix
=
"spring.data.rest"
)
@ConfigurationProperties
(
prefix
=
"spring.data.rest"
)
@Override
@Override
...
@@ -57,5 +64,12 @@ public class RepositoryRestMvcAutoConfiguration {
...
@@ -57,5 +64,12 @@ public class RepositoryRestMvcAutoConfiguration {
return
super
.
config
();
return
super
.
config
();
}
}
@Override
protected
void
configureJacksonObjectMapper
(
ObjectMapper
objectMapper
)
{
if
(
this
.
objectMapperBuilder
!=
null
)
{
this
.
objectMapperBuilder
.
configure
(
objectMapper
);
}
}
}
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java
View file @
261b3afc
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
data
.
rest
;
package
org
.
springframework
.
boot
.
autoconfigure
.
data
.
rest
;
import
java.net.URI
;
import
java.net.URI
;
import
java.util.Date
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -27,15 +28,20 @@ import org.springframework.boot.autoconfigure.data.jpa.city.City;
...
@@ -27,15 +28,20 @@ import org.springframework.boot.autoconfigure.data.jpa.city.City;
import
org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration
;
import
org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration
;
import
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
;
import
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.data.rest.core.config.RepositoryRestConfiguration
;
import
org.springframework.data.rest.core.config.RepositoryRestConfiguration
;
import
org.springframework.data.rest.webmvc.BaseUri
;
import
org.springframework.data.rest.webmvc.BaseUri
;
import
org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
;
import
org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration
;
import
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
;
import
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
...
@@ -43,6 +49,7 @@ import static org.junit.Assert.assertNotNull;
...
@@ -43,6 +49,7 @@ import static org.junit.Assert.assertNotNull;
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
*
*
* @author Rob Winch
* @author Rob Winch
* @author Andy Wilkinson
*/
*/
public
class
RepositoryRestMvcAutoConfigurationTests
{
public
class
RepositoryRestMvcAutoConfigurationTests
{
...
@@ -85,6 +92,23 @@ public class RepositoryRestMvcAutoConfigurationTests {
...
@@ -85,6 +92,23 @@ public class RepositoryRestMvcAutoConfigurationTests {
}
}
@Test
public
void
objectMappersAreConfiguredUsingObjectMapperBuilder
()
throws
JsonProcessingException
{
load
(
TestConfigurationWithObjectMapperBuilder
.
class
);
assertThatDateIsFormattedCorrectly
(
"halObjectMapper"
);
assertThatDateIsFormattedCorrectly
(
"objectMapper"
);
}
public
void
assertThatDateIsFormattedCorrectly
(
String
beanName
)
throws
JsonProcessingException
{
ObjectMapper
objectMapper
=
this
.
context
.
getBean
(
beanName
,
ObjectMapper
.
class
);
assertEquals
(
"\"2014-10\""
,
objectMapper
.
writeValueAsString
(
new
Date
(
1413387983267L
)));
}
private
void
load
(
Class
<?>
config
,
String
...
environment
)
{
private
void
load
(
Class
<?>
config
,
String
...
environment
)
{
AnnotationConfigWebApplicationContext
applicationContext
=
new
AnnotationConfigWebApplicationContext
();
AnnotationConfigWebApplicationContext
applicationContext
=
new
AnnotationConfigWebApplicationContext
();
applicationContext
.
setServletContext
(
new
MockServletContext
());
applicationContext
.
setServletContext
(
new
MockServletContext
());
...
@@ -110,4 +134,17 @@ public class RepositoryRestMvcAutoConfigurationTests {
...
@@ -110,4 +134,17 @@ public class RepositoryRestMvcAutoConfigurationTests {
}
}
@Configuration
@TestAutoConfigurationPackage
(
City
.
class
)
@EnableWebMvc
static
class
TestConfigurationWithObjectMapperBuilder
{
@Bean
public
Jackson2ObjectMapperBuilder
objectMapperBuilder
()
{
Jackson2ObjectMapperBuilder
objectMapperBuilder
=
new
Jackson2ObjectMapperBuilder
();
objectMapperBuilder
.
simpleDateFormat
(
"yyyy-MM"
);
return
objectMapperBuilder
;
}
}
}
}
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