Skip to content

Commit 7b8469f

Browse files
rursprungreta
authored andcommitted
jackson ObjectMapper: auto-detect modules (opensearch-project#1643)
jackson 2.x still supports java 7 and thus does not automatically support java 8 classes. this will change with jackson 3.x (see e.g. this [PR adding JSR310 date support] to 3.x). PR opensearch-project#251 added documentation on how a custom `ObjectMapper` can be registered - but there's no reason why we can't just enable auto-detection centrally. the docs mention that performance should be considered, but this is IMHO not relevant here since we only construct it once and then keep the same `ObjectMapper` (which in turn the docs mention as best practices). Signed-off-by: Ralph Ursprung <[email protected]> [PR adding JSR310 date support]: FasterXML/jackson-databind#5032 (cherry picked from commit c1ae512)
1 parent f78d75d commit 7b8469f

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
33

44
## [Unreleased 2.x]
55
### Added
6+
- Jackson `ObjectMapper` modules are now being auto-detected ([#1643](https://github.com/opensearch-project/opensearch-java/pull/1643))
67

78
### Dependencies
89

USER_GUIDE.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,50 @@ The Apache HttpClient 5 based transport has dependences on Apache HttpClient 5 a
9696
implementation("org.apache.httpcomponents.core5", "httpcore5", "5.2.2")
9797
```
9898

99+
```java
100+
final OpenSearchTransport transport = ApacheHttpClient5TransportBuilder
101+
.builder(httpHost)
102+
.setMapper(new JacksonJsonpMapper())
103+
.setHttpClientConfigCallback(new ApacheHttpClient5TransportBuilder.HttpClientConfigCallback() {
104+
@Override
105+
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
106+
return httpClientBuilder.setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2);
107+
}
108+
})
109+
.build();
110+
OpenSearchClient client = new OpenSearchClient(transport);
111+
```
112+
113+
See [SampleClient.java](./samples/src/main/java/org/opensearch/client/samples/SampleClient.java) for a working sample.
114+
115+
#### Using `RestClientTransport` (deprecated)
116+
117+
```java
118+
import org.apache.hc.core5.http.HttpHost;
119+
120+
final HttpHost[] hosts = new HttpHost[] {
121+
new HttpHost("http", "localhost", 9200)
122+
};
123+
124+
// Initialize the client with SSL and TLS enabled
125+
final RestClient restClient = RestClient
126+
.builder(hosts)
127+
.build();
128+
129+
OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
130+
OpenSearchClient client = new OpenSearchClient(transport);
131+
```
132+
133+
The `JacksonJsonpMapper` class (2.x versions) only supports Java 7 objects by default. [Java 8 modules](https://github.com/FasterXML/jackson-modules-java8) to support JDK8 classes such as the Date and Time API (JSR-310), `Optional`, and more can be used by including [the additional datatype dependency](https://github.com/FasterXML/jackson-modules-java8#usage) and adding the module. Auto-detection for these modules is enabled, adding them to the classpath is enough.
134+
You can also provide your own `ObjectMapper` instance if you need to pre-configure it differently, for example:
135+
136+
```java
137+
OpenSearchTransport transport = new RestClientTransport(restClient,
138+
new JacksonJsonpMapper(new ObjectMapper().findAndRegisterModules().configure(SerializationFeature.INDENT_OUTPUT, true)));
139+
OpenSearchClient client = new OpenSearchClient(transport);
140+
>>>>>>> c1ae51280 (jackson `ObjectMapper`: auto-detect modules (#1643))
141+
```
142+
99143
Upcoming OpenSearch `3.0.0` release brings HTTP/2 support and as such, the `RestClientTransport` would switch to HTTP/2 if available (for both HTTPS and/or HTTP protocols). The desired protocol could be forced using `RestClientBuilder.HttpClientConfigCallback`.
100144

101145
See [SampleClient.java](./samples/src/main/java/org/opensearch/client/samples/SampleClient.java) for a working sample.

java-client/src/main/java/org/opensearch/client/json/jackson/JacksonJsonpMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public JacksonJsonpMapper(ObjectMapper objectMapper, JsonFactory jsonFactory) {
6363

6464
public JacksonJsonpMapper() {
6565
this(
66-
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false).setSerializationInclusion(JsonInclude.Include.NON_NULL)
66+
new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, false)
67+
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
68+
.findAndRegisterModules()
6769
);
6870
}
6971

0 commit comments

Comments
 (0)