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
8a6e79dc
Commit
8a6e79dc
authored
Jan 10, 2021
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Configure Couchbase to use the application's ObjectMapper
Closes gh-24616
parent
45f298bc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
2 deletions
+73
-2
CouchbaseAutoConfiguration.java
...t/autoconfigure/couchbase/CouchbaseAutoConfiguration.java
+41
-1
CouchbaseAutoConfigurationTests.java
...oconfigure/couchbase/CouchbaseAutoConfigurationTests.java
+32
-1
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java
View file @
8a6e79dc
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -27,18 +27,24 @@ import com.couchbase.client.core.env.SecurityConfig;
import
com.couchbase.client.core.env.TimeoutConfig
;
import
com.couchbase.client.java.Cluster
;
import
com.couchbase.client.java.ClusterOptions
;
import
com.couchbase.client.java.codec.JacksonJsonSerializer
;
import
com.couchbase.client.java.env.ClusterEnvironment
;
import
com.couchbase.client.java.env.ClusterEnvironment.Builder
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
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.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate
;
import
org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Timeouts
;
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.core.Ordered
;
import
org.springframework.util.ResourceUtils
;
/**
...
...
@@ -50,6 +56,7 @@ import org.springframework.util.ResourceUtils;
* @since 1.4.0
*/
@Configuration
(
proxyBeanMethods
=
false
)
@AutoConfigureAfter
(
JacksonAutoConfiguration
.
class
)
@ConditionalOnClass
(
Cluster
.
class
)
@ConditionalOnProperty
(
"spring.couchbase.connection-string"
)
@EnableConfigurationProperties
(
CouchbaseProperties
.
class
)
...
...
@@ -111,4 +118,37 @@ public class CouchbaseAutoConfiguration {
return
store
;
}
@Configuration
(
proxyBeanMethods
=
false
)
@ConditionalOnClass
(
ObjectMapper
.
class
)
static
class
JacksonConfiguration
{
@Bean
@ConditionalOnSingleCandidate
(
ObjectMapper
.
class
)
ClusterEnvironmentBuilderCustomizer
cluster
(
ObjectMapper
objectMapper
)
{
return
new
JacksonClusterEnvironmentBuilderCustomizer
(
objectMapper
);
}
}
private
static
final
class
JacksonClusterEnvironmentBuilderCustomizer
implements
ClusterEnvironmentBuilderCustomizer
,
Ordered
{
private
final
ObjectMapper
objectMapper
;
private
JacksonClusterEnvironmentBuilderCustomizer
(
ObjectMapper
objectMapper
)
{
this
.
objectMapper
=
objectMapper
;
}
@Override
public
void
customize
(
Builder
builder
)
{
builder
.
jsonSerializer
(
JacksonJsonSerializer
.
create
(
this
.
objectMapper
));
}
@Override
public
int
getOrder
()
{
return
0
;
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfigurationTests.java
View file @
8a6e79dc
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
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.
...
...
@@ -23,15 +23,20 @@ import com.couchbase.client.core.env.IoConfig;
import
com.couchbase.client.core.env.SecurityConfig
;
import
com.couchbase.client.core.env.TimeoutConfig
;
import
com.couchbase.client.java.Cluster
;
import
com.couchbase.client.java.codec.JacksonJsonSerializer
;
import
com.couchbase.client.java.codec.JsonSerializer
;
import
com.couchbase.client.java.env.ClusterEnvironment
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for {@link CouchbaseAutoConfiguration}.
...
...
@@ -60,6 +65,32 @@ class CouchbaseAutoConfigurationTests {
});
}
@Test
void
environmentUseObjectMapperByDefault
()
{
this
.
contextRunner
.
withUserConfiguration
(
CouchbaseTestConfiguration
.
class
)
.
withConfiguration
(
AutoConfigurations
.
of
(
JacksonAutoConfiguration
.
class
))
.
withPropertyValues
(
"spring.couchbase.connection-string=localhost"
).
run
((
context
)
->
{
ClusterEnvironment
env
=
context
.
getBean
(
ClusterEnvironment
.
class
);
JsonSerializer
serializer
=
env
.
jsonSerializer
();
assertThat
(
serializer
).
isInstanceOf
(
JacksonJsonSerializer
.
class
)
.
hasFieldOrPropertyWithValue
(
"mapper"
,
context
.
getBean
(
ObjectMapper
.
class
));
});
}
@Test
void
customizeJsonSerializer
()
{
JsonSerializer
customJsonSerializer
=
mock
(
JsonSerializer
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
CouchbaseTestConfiguration
.
class
)
.
withConfiguration
(
AutoConfigurations
.
of
(
JacksonAutoConfiguration
.
class
))
.
withBean
(
ClusterEnvironmentBuilderCustomizer
.
class
,
()
->
(
builder
)
->
builder
.
jsonSerializer
(
customJsonSerializer
))
.
withPropertyValues
(
"spring.couchbase.connection-string=localhost"
).
run
((
context
)
->
{
ClusterEnvironment
env
=
context
.
getBean
(
ClusterEnvironment
.
class
);
JsonSerializer
serializer
=
env
.
jsonSerializer
();
assertThat
(
serializer
).
isSameAs
(
customJsonSerializer
);
});
}
@Test
void
customizeEnvIo
()
{
testClusterEnvironment
((
env
)
->
{
...
...
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