forked from spring-projects/spring-pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow custom object mapper for messages
This commit adds support for a user-provided Jackson ObjectMapper to be used when de/serializing JSON messages. Additionally, adds Gradle test fixtures to the spring-pulsar module and deprecates the UserRecord and UserPojo in spring-pulsar-test in favor of their equivalent in the test fixture. See spring-projects#723
- Loading branch information
Showing
32 changed files
with
864 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...sar-docs/src/main/antora/modules/ROOT/pages/reference/custom-object-mapper.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[[custom-object-mapper]] | ||
= Custom Object Mapper | ||
include::../attributes/attributes.adoc[] | ||
|
||
Pulsar uses an internal Jackson `ObjectMapper` when de/serializing JSON messages. | ||
If you instead want to provide your own object mapper instance, you can register a `SchemaResolverCustomizer` and set your mapper on the `DefaultSchemaResolver` as follows: | ||
|
||
[source,java,indent=0,subs="verbatim"] | ||
---- | ||
@Bean | ||
SchemaResolverCustomizer<DefaultSchemaResolver> schemaResolverCustomizer() { | ||
return (DefaultSchemaResolver schemaResolver) -> { | ||
var myObjectMapper = obtainMyObjectMapper(); | ||
schemaResolver.setObjectMapper(myObjectMapper); | ||
}; | ||
} | ||
---- | ||
|
||
This results in your object mapper being used to de/serialize all JSON messages that go through the schema resolution process (i.e. in cases where you do not pass a schema in directly when producing/consuming messages). | ||
|
||
Under the hood, the resolver creates a special JSON schema which leverages the custom mapper and is used as the schema for all resolved JSON messages. | ||
|
||
If you need to pass schema instances directly you can use the `JSONSchemaUtil` to create schemas that respect the custom mapper. | ||
The following example shows how to do this when sending a message with the `PulsarTemplate` variant that takes a schema parameter: | ||
|
||
[source,java,indent=0,subs="verbatim"] | ||
---- | ||
void sendMessage(PulsarTemplate<MyPojo> template, MyPojo toSend) { | ||
var myObjectMapper = obtainMyObjectMapper(); | ||
var schema = JSONSchemaUtil.schemaForTypeWithObjectMapper(MyPojo.class, myObjectMapper); | ||
template.send(toSend, schema); | ||
} | ||
---- | ||
|
||
|
||
[CAUTION] | ||
==== | ||
Pulsar configures its default object mapper in a particular way. | ||
Unless you have a specific reason to not do so, it is highly recommended that you configure your mapper with these same options as follows: | ||
[source,java,indent=0,subs="verbatim"] | ||
---- | ||
myObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
myObjectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, false); | ||
myObjectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
---- | ||
==== | ||
NOTE: A later version of the framework may instead provide a customizer that operates on the default mapper rather than requiring a separate instance. |
10 changes: 10 additions & 0 deletions
10
spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.