|
16 | 16 |
|
17 | 17 | package org.springframework.data.couchbase.config;
|
18 | 18 |
|
19 |
| -import static com.couchbase.client.java.ClusterOptions.*; |
| 19 | +import static com.couchbase.client.java.ClusterOptions.clusterOptions; |
20 | 20 |
|
21 | 21 | import java.util.Collections;
|
22 | 22 | import java.util.HashSet;
|
23 | 23 | import java.util.Set;
|
24 | 24 |
|
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
25 | 26 | import org.springframework.beans.factory.config.BeanDefinition;
|
26 | 27 | import org.springframework.context.annotation.Bean;
|
27 | 28 | import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
|
48 | 49 | import org.springframework.util.StringUtils;
|
49 | 50 |
|
50 | 51 | import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.DeserializationFeature;
|
| 52 | +import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.Module; |
| 53 | +import com.couchbase.client.core.encryption.CryptoManager; |
51 | 54 | import com.couchbase.client.core.env.Authenticator;
|
52 | 55 | import com.couchbase.client.core.env.PasswordAuthenticator;
|
| 56 | +import com.couchbase.client.core.error.CouchbaseException; |
53 | 57 | import com.couchbase.client.java.Cluster;
|
| 58 | +import com.couchbase.client.java.codec.JacksonJsonSerializer; |
| 59 | +import com.couchbase.client.java.encryption.databind.jackson.EncryptionModule; |
54 | 60 | import com.couchbase.client.java.env.ClusterEnvironment;
|
55 | 61 | import com.couchbase.client.java.json.JacksonTransformers;
|
| 62 | +import com.couchbase.client.java.json.JsonValueModule; |
| 63 | +import com.couchbase.client.java.json.RepackagedJsonValueModule; |
| 64 | +import com.fasterxml.jackson.databind.ObjectMapper; |
56 | 65 |
|
57 | 66 | /**
|
58 | 67 | * Base class for Spring Data Couchbase configuration using JavaConfig.
|
|
65 | 74 | @Configuration
|
66 | 75 | public abstract class AbstractCouchbaseConfiguration {
|
67 | 76 |
|
| 77 | + @Autowired ObjectMapper couchbaseObjectMapper; |
| 78 | + |
68 | 79 | /**
|
69 | 80 | * The connection string which allows the SDK to connect to the cluster.
|
70 | 81 | * <p>
|
71 |
| - * Note that the connection string can take many forms, in its simplest it is just a single hostname |
72 |
| - * like "127.0.0.1". Please refer to the couchbase Java SDK documentation for all the different |
73 |
| - * possibilities and options. |
| 82 | + * Note that the connection string can take many forms, in its simplest it is just a single hostname like "127.0.0.1". |
| 83 | + * Please refer to the couchbase Java SDK documentation for all the different possibilities and options. |
74 | 84 | */
|
75 | 85 | public abstract String getConnectionString();
|
76 | 86 |
|
@@ -130,6 +140,10 @@ public Cluster couchbaseCluster(ClusterEnvironment couchbaseClusterEnvironment)
|
130 | 140 | @Bean(destroyMethod = "shutdown")
|
131 | 141 | public ClusterEnvironment couchbaseClusterEnvironment() {
|
132 | 142 | ClusterEnvironment.Builder builder = ClusterEnvironment.builder();
|
| 143 | + if (!nonShadowedJacksonPresent()) { |
| 144 | + throw new CouchbaseException("non-shadowed Jackson not present"); |
| 145 | + } |
| 146 | + builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper)); |
133 | 147 | configureEnvironment(builder);
|
134 | 148 | return builder.build();
|
135 | 149 | }
|
@@ -273,6 +287,25 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
|
273 | 287 | return mappingContext;
|
274 | 288 | }
|
275 | 289 |
|
| 290 | + /** |
| 291 | + * Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment |
| 292 | + * |
| 293 | + * @throws Exception on Bean construction failure. |
| 294 | + * @return ObjectMapper |
| 295 | + */ |
| 296 | + |
| 297 | + @Bean |
| 298 | + public ObjectMapper couchbaseObjectMapper() { |
| 299 | + ObjectMapper mapper = new ObjectMapper(); |
| 300 | + mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 301 | + mapper.registerModule(new JsonValueModule()); |
| 302 | + CryptoManager cryptoManager = null; |
| 303 | + if (cryptoManager != null) { |
| 304 | + mapper.registerModule(new EncryptionModule(cryptoManager)); |
| 305 | + } |
| 306 | + return mapper; |
| 307 | + } |
| 308 | + |
276 | 309 | /**
|
277 | 310 | * Configure whether to automatically create indices for domain types by deriving the from the entity or not.
|
278 | 311 | */
|
@@ -327,4 +360,14 @@ protected FieldNamingStrategy fieldNamingStrategy() {
|
327 | 360 | return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy()
|
328 | 361 | : PropertyNameFieldNamingStrategy.INSTANCE;
|
329 | 362 | }
|
| 363 | + |
| 364 | + private boolean nonShadowedJacksonPresent() { |
| 365 | + try { |
| 366 | + JacksonJsonSerializer.preflightCheck(); |
| 367 | + return true; |
| 368 | + } catch (Throwable t) { |
| 369 | + return false; |
| 370 | + } |
| 371 | + } |
| 372 | + |
330 | 373 | }
|
0 commit comments