Polishing.
This commit is contained in:
@@ -43,17 +43,39 @@ include::guides/boot-mongo.adoc[tags=config,leveloffset=+3]
|
||||
==== Session serialization mechanisms
|
||||
|
||||
To be able to persist session objects in MongoDB we need to provide the serialization/deserialization mechanism.
|
||||
Depending on your classpath Spring Session will choose one of two build-in converters:
|
||||
|
||||
* `JacksonMongoSessionConverter` when `ObjectMapper` class is available, or
|
||||
* `JdkMongoSessionConverter` otherwise.
|
||||
By default, Spring Session MongoDB will use `JdkMongoSessionConverter`.
|
||||
|
||||
However, you may switch to `JacksonMongoSessionConverter` by merely adding the following code to your Boot app:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
JacksonMongoSessionConverter mongoSessionConverter() {
|
||||
return new JacksonMongoSessionConverter();
|
||||
}
|
||||
----
|
||||
|
||||
===== JacksonMongoSessionConverter
|
||||
|
||||
This mechanism uses Jackson to serialize session objects to/from JSON.
|
||||
`JacksonMongoSessionConverter` will be the default when Jackson is detected on the classpath and the user has not explicitly registered a `AbstractMongoSessionConverter` Bean.
|
||||
|
||||
If you would like to provide custom Jackson modules you can do it by explicitly registering `JacksonMongoSessionConverter`:
|
||||
By creating the following bean:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
JacksonMongoSessionConverter mongoSessionConverter() {
|
||||
return new JacksonMongoSessionConverter();
|
||||
}
|
||||
----
|
||||
|
||||
...you are able to switch from the default (JDK-based serialization) to using Jackson.
|
||||
|
||||
IMPORTANT: If you are integrating with Spring Security (by storing your sessions in MongoDB), this configuration will
|
||||
register the proper whitelisted components so Spring Security works properly.
|
||||
|
||||
If you would like to provide custom Jackson modules you can do it by explicitly registering modules as shown below:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@@ -65,12 +87,8 @@ include::{code-dir}/src/test/java/org/springframework/session/data/mongo/integra
|
||||
`JdkMongoSessionConverter` uses standard Java serialization to persist session attributes map to MongoDB in a binary form.
|
||||
However, standard session elements like id, access time, etc are still written as a plain Mongo objects and can be read and queried without additional effort.
|
||||
`JdkMongoSessionConverter` is used if Jackson is not on the classpath and no explicit `AbstractMongoSessionConverter` Bean has been defined.
|
||||
You can explicitly register `JdkMongoSessionConverter` by defining it as a Bean.
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
include::{code-dir}/src/test/java/org/springframework/session/data/mongo/integration/MongoRepositoryJdkSerializationITest.java[tag=sample]
|
||||
----
|
||||
There is no need to explicitly register a `JdkMongoSessionConverter` bean since it's the default strategy.
|
||||
|
||||
There is also a constructor taking `Serializer` and `Deserializer` objects, allowing you to pass custom implementations, which is especially important when you want to use non-default classloader.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user