Skip to content

Commit 1d8867b

Browse files
authored
Make JsonpMapper a customizable bean.
Original Pull Request #2580 Closes #2566
1 parent f558822 commit 1d8867b

File tree

5 files changed

+91
-24
lines changed

5 files changed

+91
-24
lines changed

src/main/asciidoc/reference/elasticsearch-clients.adoc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,31 @@ public class MyClientConfig extends ElasticsearchConfiguration {
3232
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
3333
====
3434

35+
The `ElasticsearchConfiguration` class allows further configuration by overriding for example the `jsonpMapper()` or `transportOptions()` methods.
36+
37+
3538
The following beans can then be injected in other Spring components:
3639

3740
====
3841
[source,java]
3942
----
40-
@Autowired
43+
import org.springframework.beans.factory.annotation.Autowired;@Autowired
4144
ElasticsearchOperations operations; <.>
4245
4346
@Autowired
4447
ElasticsearchClient elasticsearchClient; <.>
4548
4649
@Autowired
4750
RestClient restClient; <.>
51+
52+
@Autowired
53+
JsonpMapper jsonpMapper; <.>
4854
----
4955
5056
<.> an implementation of `ElasticsearchOperations`
5157
<.> the `co.elastic.clients.elasticsearch.ElasticsearchClient` that is used.
5258
<.> the low level `RestClient` from the Elasticsearch libraries
59+
<.> the `JsonpMapper` user by the Elasticsearch `Transport`
5360
====
5461

5562
Basically one should just use the `ElasticsearchOperations` to interact with the Elasticsearch cluster.
@@ -79,6 +86,8 @@ public class MyClientConfig extends ReactiveElasticsearchConfiguration {
7986
<.> for a detailed description of the builder methods see <<elasticsearch.clients.configuration>>
8087
====
8188

89+
The `ReactiveElasticsearchConfiguration` class allows further configuration by overriding for example the `jsonpMapper()` or `transportOptions()` methods.
90+
8291
The following beans can then be injected in other Spring components:
8392

8493
====
@@ -92,6 +101,9 @@ ReactiveElasticsearchClient elasticsearchClient; <.>
92101
93102
@Autowired
94103
RestClient restClient; <.>
104+
105+
@Autowired
106+
JsonpMapper jsonpMapper; <.>
95107
----
96108
97109
the following can be injected:
@@ -100,6 +112,7 @@ the following can be injected:
100112
<.> the `org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient` that is used.
101113
This is a reactive implementation based on the Elasticsearch client implementation.
102114
<.> the low level `RestClient` from the Elasticsearch libraries
115+
<.> the `JsonpMapper` user by the Elasticsearch `Transport`
103116
====
104117

105118
Basically one should just use the `ReactiveElasticsearchOperations` to interact with the Elasticsearch cluster.

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchClients.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.elasticsearch.client.elc;
1717

1818
import co.elastic.clients.elasticsearch.ElasticsearchClient;
19+
import co.elastic.clients.json.JsonpMapper;
1920
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
2021
import co.elastic.clients.transport.ElasticsearchTransport;
2122
import co.elastic.clients.transport.TransportOptions;
@@ -24,8 +25,6 @@
2425
import co.elastic.clients.transport.rest_client.RestClientOptions;
2526
import co.elastic.clients.transport.rest_client.RestClientTransport;
2627

27-
import java.io.ByteArrayOutputStream;
28-
import java.io.IOException;
2928
import java.net.InetSocketAddress;
3029
import java.time.Duration;
3130
import java.util.Arrays;
@@ -60,11 +59,12 @@ public final class ElasticsearchClients {
6059
/**
6160
* Name of whose value can be used to correlate log messages for this request.
6261
*/
63-
private static final String LOG_ID_ATTRIBUTE = ElasticsearchClients.class.getName() + ".LOG_ID";
6462
private static final String X_SPRING_DATA_ELASTICSEARCH_CLIENT = "X-SpringDataElasticsearch-Client";
6563
private static final String IMPERATIVE_CLIENT = "imperative";
6664
private static final String REACTIVE_CLIENT = "reactive";
6765

66+
private static final JsonpMapper DEFAULT_JSONP_MAPPER = new JacksonJsonpMapper();
67+
6868
/**
6969
* Creates a new {@link ReactiveElasticsearchClient}
7070
*
@@ -75,7 +75,7 @@ public static ReactiveElasticsearchClient createReactive(ClientConfiguration cli
7575

7676
Assert.notNull(clientConfiguration, "clientConfiguration must not be null");
7777

78-
return createReactive(getRestClient(clientConfiguration), null);
78+
return createReactive(getRestClient(clientConfiguration), null, DEFAULT_JSONP_MAPPER);
7979
}
8080

8181
/**
@@ -90,7 +90,24 @@ public static ReactiveElasticsearchClient createReactive(ClientConfiguration cli
9090

9191
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!");
9292

93-
return createReactive(getRestClient(clientConfiguration), transportOptions);
93+
return createReactive(getRestClient(clientConfiguration), transportOptions, DEFAULT_JSONP_MAPPER);
94+
}
95+
96+
/**
97+
* Creates a new {@link ReactiveElasticsearchClient}
98+
*
99+
* @param clientConfiguration configuration options, must not be {@literal null}.
100+
* @param transportOptions options to be added to each request.
101+
* @param jsonpMapper the JsonpMapper to use
102+
* @return the {@link ReactiveElasticsearchClient}
103+
*/
104+
public static ReactiveElasticsearchClient createReactive(ClientConfiguration clientConfiguration,
105+
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
106+
107+
Assert.notNull(clientConfiguration, "ClientConfiguration must not be null!");
108+
Assert.notNull(jsonpMapper, "jsonpMapper must not be null");
109+
110+
return createReactive(getRestClient(clientConfiguration), transportOptions, jsonpMapper);
94111
}
95112

96113
/**
@@ -100,7 +117,7 @@ public static ReactiveElasticsearchClient createReactive(ClientConfiguration cli
100117
* @return the {@link ReactiveElasticsearchClient}
101118
*/
102119
public static ReactiveElasticsearchClient createReactive(RestClient restClient) {
103-
return createReactive(restClient, null);
120+
return createReactive(restClient, null, DEFAULT_JSONP_MAPPER);
104121
}
105122

106123
/**
@@ -111,8 +128,9 @@ public static ReactiveElasticsearchClient createReactive(RestClient restClient)
111128
* @return the {@link ReactiveElasticsearchClient}
112129
*/
113130
public static ReactiveElasticsearchClient createReactive(RestClient restClient,
114-
@Nullable TransportOptions transportOptions) {
115-
return new ReactiveElasticsearchClient(getElasticsearchTransport(restClient, REACTIVE_CLIENT, transportOptions));
131+
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
132+
return new ReactiveElasticsearchClient(
133+
getElasticsearchTransport(restClient, REACTIVE_CLIENT, transportOptions, jsonpMapper));
116134
}
117135

118136
/**
@@ -122,7 +140,7 @@ public static ReactiveElasticsearchClient createReactive(RestClient restClient,
122140
* @return the {@link ElasticsearchClient}
123141
*/
124142
public static ElasticsearchClient createImperative(ClientConfiguration clientConfiguration) {
125-
return createImperative(getRestClient(clientConfiguration), null);
143+
return createImperative(getRestClient(clientConfiguration), null, DEFAULT_JSONP_MAPPER);
126144
}
127145

128146
/**
@@ -134,7 +152,7 @@ public static ElasticsearchClient createImperative(ClientConfiguration clientCon
134152
*/
135153
public static ElasticsearchClient createImperative(ClientConfiguration clientConfiguration,
136154
TransportOptions transportOptions) {
137-
return createImperative(getRestClient(clientConfiguration), transportOptions);
155+
return createImperative(getRestClient(clientConfiguration), transportOptions, DEFAULT_JSONP_MAPPER);
138156
}
139157

140158
/**
@@ -144,22 +162,24 @@ public static ElasticsearchClient createImperative(ClientConfiguration clientCon
144162
* @return the {@link ElasticsearchClient}
145163
*/
146164
public static ElasticsearchClient createImperative(RestClient restClient) {
147-
return createImperative(restClient, null);
165+
return createImperative(restClient, null, DEFAULT_JSONP_MAPPER);
148166
}
149167

150168
/**
151169
* Creates a new imperative {@link ElasticsearchClient}
152170
*
153171
* @param restClient the RestClient to use
154172
* @param transportOptions options to be added to each request.
173+
* @param jsonpMapper the mapper for the transport to use
155174
* @return the {@link ElasticsearchClient}
156175
*/
157-
public static ElasticsearchClient createImperative(RestClient restClient,
158-
@Nullable TransportOptions transportOptions) {
176+
public static ElasticsearchClient createImperative(RestClient restClient, @Nullable TransportOptions transportOptions,
177+
JsonpMapper jsonpMapper) {
159178

160179
Assert.notNull(restClient, "restClient must not be null");
161180

162-
ElasticsearchTransport transport = getElasticsearchTransport(restClient, IMPERATIVE_CLIENT, transportOptions);
181+
ElasticsearchTransport transport = getElasticsearchTransport(restClient, IMPERATIVE_CLIENT, transportOptions,
182+
jsonpMapper);
163183

164184
return new AutoCloseableElasticsearchClient(transport);
165185
}
@@ -236,7 +256,7 @@ private static RestClientBuilder getRestClientBuilder(ClientConfiguration client
236256
}
237257

238258
private static ElasticsearchTransport getElasticsearchTransport(RestClient restClient, String clientType,
239-
@Nullable TransportOptions transportOptions) {
259+
@Nullable TransportOptions transportOptions, JsonpMapper jsonpMapper) {
240260

241261
TransportOptions.Builder transportOptionsBuilder = transportOptions != null ? transportOptions.toBuilder()
242262
: new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
@@ -260,7 +280,7 @@ private static ElasticsearchTransport getElasticsearchTransport(RestClient restC
260280
TransportOptions transportOptionsWithHeader = transportOptionsBuilder
261281
.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType).build();
262282

263-
return new RestClientTransport(restClient, new JacksonJsonpMapper(), transportOptionsWithHeader);
283+
return new RestClientTransport(restClient, jsonpMapper, transportOptionsWithHeader);
264284
}
265285

266286
private static List<String> formattedHosts(List<InetSocketAddress> hosts, boolean useSsl) {

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchConfiguration.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
package org.springframework.data.elasticsearch.client.elc;
1717

1818
import co.elastic.clients.elasticsearch.ElasticsearchClient;
19+
import co.elastic.clients.json.JsonpMapper;
20+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
1921
import co.elastic.clients.transport.TransportOptions;
2022
import co.elastic.clients.transport.rest_client.RestClientOptions;
2123

2224
import org.elasticsearch.client.RequestOptions;
2325
import org.elasticsearch.client.RestClient;
26+
import org.jetbrains.annotations.NotNull;
2427
import org.springframework.context.annotation.Bean;
2528
import org.springframework.data.elasticsearch.client.ClientConfiguration;
2629
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
@@ -42,7 +45,7 @@ public abstract class ElasticsearchConfiguration extends ElasticsearchConfigurat
4245
*
4346
* @return configuration, must not be {@literal null}
4447
*/
45-
@Bean(name="elasticsearchClientConfiguration")
48+
@Bean(name = "elasticsearchClientConfiguration")
4649
public abstract ClientConfiguration clientConfiguration();
4750

4851
/**
@@ -63,14 +66,15 @@ public RestClient elasticsearchRestClient(ClientConfiguration clientConfiguratio
6366
* Provides the {@link ElasticsearchClient} to be used.
6467
*
6568
* @param restClient the low level RestClient to use
69+
* @param jsonpMapper the JsonpMapper to use
6670
* @return ElasticsearchClient instance
6771
*/
6872
@Bean
69-
public ElasticsearchClient elasticsearchClient(RestClient restClient) {
73+
public ElasticsearchClient elasticsearchClient(RestClient restClient, JsonpMapper jsonpMapper) {
7074

7175
Assert.notNull(restClient, "restClient must not be null");
7276

73-
return ElasticsearchClients.createImperative(restClient, transportOptions());
77+
return ElasticsearchClients.createImperative(restClient, transportOptions(), jsonpMapper);
7478
}
7579

7680
/**
@@ -89,6 +93,18 @@ public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter el
8993
return template;
9094
}
9195

96+
/**
97+
* Provides the JsonpMapper bean that is used in the {@link #elasticsearchClient(RestClient, JsonpMapper)} method.
98+
* This can be adapted by overriding tge {@link #getJsonpMapper()} method.
99+
*
100+
* @return the {@link JsonpMapper} to use
101+
* @since 5.2
102+
*/
103+
@Bean
104+
public JsonpMapper jsonpMapper() {
105+
return new JacksonJsonpMapper();
106+
}
107+
92108
/**
93109
* @return the options that should be added to every request. Must not be {@literal null}
94110
*/

src/main/java/org/springframework/data/elasticsearch/client/elc/ReactiveElasticsearchConfiguration.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.elasticsearch.client.elc;
1717

18+
import co.elastic.clients.json.JsonpMapper;
19+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
1820
import co.elastic.clients.transport.TransportOptions;
1921
import co.elastic.clients.transport.rest_client.RestClientOptions;
2022

@@ -41,7 +43,7 @@ public abstract class ReactiveElasticsearchConfiguration extends ElasticsearchCo
4143
*
4244
* @return configuration, must not be {@literal null}
4345
*/
44-
@Bean(name="elasticsearchClientConfiguration")
46+
@Bean(name = "elasticsearchClientConfiguration")
4547
public abstract ClientConfiguration clientConfiguration();
4648

4749
/**
@@ -65,11 +67,11 @@ public RestClient elasticsearchRestClient(ClientConfiguration clientConfiguratio
6567
* @return ReactiveElasticsearchClient instance.
6668
*/
6769
@Bean
68-
public ReactiveElasticsearchClient reactiveElasticsearchClient(RestClient restClient) {
70+
public ReactiveElasticsearchClient reactiveElasticsearchClient(RestClient restClient, JsonpMapper jsonpMapper) {
6971

7072
Assert.notNull(restClient, "restClient must not be null");
7173

72-
return ElasticsearchClients.createReactive(restClient, transportOptions());
74+
return ElasticsearchClients.createReactive(restClient, transportOptions(), jsonpMapper);
7375
}
7476

7577
/**
@@ -88,6 +90,18 @@ public ReactiveElasticsearchOperations reactiveElasticsearchOperations(Elasticse
8890
return template;
8991
}
9092

93+
/**
94+
* Provides the JsonpMapper that is used in the {@link #reactiveElasticsearchClient(RestClient, JsonpMapper)} method
95+
* and exposes it as a bean.
96+
*
97+
* @return the {@link JsonpMapper} to use
98+
* @since 5.2
99+
*/
100+
@Bean
101+
public JsonpMapper jsonpMapper() {
102+
return new JacksonJsonpMapper();
103+
}
104+
91105
/**
92106
* @return the options that should be added to every request. Must not be {@literal null}
93107
*/

src/test/java/org/springframework/data/elasticsearch/client/elc/DevTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import co.elastic.clients.elasticsearch.indices.GetIndicesSettingsRequest;
3131
import co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse;
3232
import co.elastic.clients.elasticsearch.indices.IndexSettings;
33+
import co.elastic.clients.json.JsonpMapper;
34+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
3335
import co.elastic.clients.transport.ElasticsearchTransport;
3436
import co.elastic.clients.transport.TransportOptions;
3537
import co.elastic.clients.transport.rest_client.RestClientOptions;
@@ -75,10 +77,12 @@ public class DevTests {
7577
private final TransportOptions transportOptions = new RestClientOptions(RequestOptions.DEFAULT).toBuilder()
7678
.addHeader("X-SpringDataElasticsearch-AlwaysThere", "true").setParameter("pretty", "true").build();
7779

80+
private final JsonpMapper jsonpMapper = new JacksonJsonpMapper();
81+
7882
private final ReactiveElasticsearchClient reactiveElasticsearchClient = ElasticsearchClients
7983
.createReactive(clientConfiguration(), transportOptions);
8084
private final ElasticsearchClient imperativeElasticsearchClient = ElasticsearchClients
81-
.createImperative(ElasticsearchClients.getRestClient(clientConfiguration()), transportOptions);
85+
.createImperative(ElasticsearchClients.getRestClient(clientConfiguration()), transportOptions, jsonpMapper);
8286

8387
@Test
8488
void someTest() throws IOException {

0 commit comments

Comments
 (0)