|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
27 | 27 | import com.couchbase.client.core.env.TimeoutConfig;
|
28 | 28 | import com.couchbase.client.java.Cluster;
|
29 | 29 | import com.couchbase.client.java.ClusterOptions;
|
| 30 | +import com.couchbase.client.java.codec.JacksonJsonSerializer; |
30 | 31 | import com.couchbase.client.java.env.ClusterEnvironment;
|
31 | 32 | import com.couchbase.client.java.env.ClusterEnvironment.Builder;
|
| 33 | +import com.fasterxml.jackson.databind.ObjectMapper; |
32 | 34 |
|
33 | 35 | import org.springframework.beans.factory.ObjectProvider;
|
| 36 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
34 | 37 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
35 | 38 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
36 | 39 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
37 | 40 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
| 41 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
38 | 42 | import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties.Timeouts;
|
| 43 | +import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; |
39 | 44 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
40 | 45 | import org.springframework.context.annotation.Bean;
|
41 | 46 | import org.springframework.context.annotation.Configuration;
|
| 47 | +import org.springframework.core.Ordered; |
42 | 48 | import org.springframework.util.ResourceUtils;
|
43 | 49 |
|
44 | 50 | /**
|
|
50 | 56 | * @since 1.4.0
|
51 | 57 | */
|
52 | 58 | @Configuration(proxyBeanMethods = false)
|
| 59 | +@AutoConfigureAfter(JacksonAutoConfiguration.class) |
53 | 60 | @ConditionalOnClass(Cluster.class)
|
54 | 61 | @ConditionalOnProperty("spring.couchbase.connection-string")
|
55 | 62 | @EnableConfigurationProperties(CouchbaseProperties.class)
|
@@ -111,4 +118,37 @@ private KeyStore loadKeyStore(String resource, String keyStorePassword) throws E
|
111 | 118 | return store;
|
112 | 119 | }
|
113 | 120 |
|
| 121 | + @Configuration(proxyBeanMethods = false) |
| 122 | + @ConditionalOnClass(ObjectMapper.class) |
| 123 | + static class JacksonConfiguration { |
| 124 | + |
| 125 | + @Bean |
| 126 | + @ConditionalOnSingleCandidate(ObjectMapper.class) |
| 127 | + ClusterEnvironmentBuilderCustomizer cluster(ObjectMapper objectMapper) { |
| 128 | + return new JacksonClusterEnvironmentBuilderCustomizer(objectMapper); |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + private static final class JacksonClusterEnvironmentBuilderCustomizer |
| 134 | + implements ClusterEnvironmentBuilderCustomizer, Ordered { |
| 135 | + |
| 136 | + private final ObjectMapper objectMapper; |
| 137 | + |
| 138 | + private JacksonClusterEnvironmentBuilderCustomizer(ObjectMapper objectMapper) { |
| 139 | + this.objectMapper = objectMapper; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void customize(Builder builder) { |
| 144 | + builder.jsonSerializer(JacksonJsonSerializer.create(this.objectMapper)); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public int getOrder() { |
| 149 | + return 0; |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
114 | 154 | }
|
0 commit comments