16
16
17
17
package org .springframework .session .data .mongo ;
18
18
19
- import java .io .IOException ;
20
- import java .util .Collections ;
21
- import java .util .Date ;
22
- import java .util .HashMap ;
23
-
24
19
import com .fasterxml .jackson .annotation .JsonAutoDetect ;
25
20
import com .fasterxml .jackson .annotation .JsonCreator ;
26
21
import com .fasterxml .jackson .annotation .JsonProperty ;
37
32
import org .bson .Document ;
38
33
import org .bson .json .JsonMode ;
39
34
import org .bson .json .JsonWriterSettings ;
40
-
41
35
import org .springframework .data .mongodb .core .query .Criteria ;
42
36
import org .springframework .data .mongodb .core .query .Query ;
43
37
import org .springframework .lang .Nullable ;
44
38
import org .springframework .security .jackson2 .SecurityJackson2Modules ;
45
39
import org .springframework .session .FindByIndexNameSessionRepository ;
46
40
import org .springframework .util .Assert ;
47
41
42
+ import java .io .IOException ;
43
+ import java .util .Collections ;
44
+ import java .util .Date ;
45
+ import java .util .HashMap ;
46
+
48
47
/**
49
48
* {@code AbstractMongoSessionConverter} implementation using Jackson.
50
49
*
51
50
* @author Jakub Kubrynski
52
51
* @author Greg Turnquist
53
52
* @author Michael Ruf
53
+ * @author TiQuan Hu
54
54
* @since 1.2
55
55
*/
56
56
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -70,17 +70,27 @@ public JacksonMongoSessionConverter() {
70
70
}
71
71
72
72
public JacksonMongoSessionConverter (Iterable <Module > modules ) {
73
-
74
73
this .objectMapper = buildObjectMapper ();
75
74
this .objectMapper .registerModules (modules );
76
75
}
77
76
78
77
public JacksonMongoSessionConverter (ObjectMapper objectMapper ) {
79
-
80
- Assert .notNull (objectMapper , "ObjectMapper can NOT be null!" );
78
+ Assert .notNull (objectMapper , "ObjectMapper can not be null!" );
81
79
this .objectMapper = objectMapper ;
82
80
}
83
81
82
+ public JacksonMongoSessionConverter (ObjectMapper objectMapper , boolean copyToUse ) {
83
+ Assert .notNull (objectMapper , "ObjectMapper can not be null!" );
84
+ if (!copyToUse ) {
85
+ configureObjectMapper (objectMapper );
86
+ this .objectMapper = objectMapper ;
87
+ return ;
88
+ }
89
+ var objectMapperCopy = objectMapper .copy ();
90
+ configureObjectMapper (objectMapperCopy );
91
+ this .objectMapper = objectMapperCopy ;
92
+ }
93
+
84
94
@ Nullable
85
95
protected Query getQueryForIndex (String indexName , Object indexValue ) {
86
96
@@ -93,9 +103,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
93
103
}
94
104
95
105
private ObjectMapper buildObjectMapper () {
96
-
97
106
ObjectMapper objectMapper = new ObjectMapper ();
107
+ this .configureObjectMapper (objectMapper );
108
+ return objectMapper ;
109
+ }
98
110
111
+ private void configureObjectMapper (ObjectMapper objectMapper ) {
99
112
// serialize fields instead of properties
100
113
objectMapper .setVisibility (PropertyAccessor .ALL , JsonAutoDetect .Visibility .NONE );
101
114
objectMapper .setVisibility (PropertyAccessor .FIELD , JsonAutoDetect .Visibility .ANY );
@@ -108,8 +121,6 @@ private ObjectMapper buildObjectMapper() {
108
121
objectMapper .registerModules (SecurityJackson2Modules .getModules (getClass ().getClassLoader ()));
109
122
objectMapper .addMixIn (MongoSession .class , MongoSessionMixin .class );
110
123
objectMapper .addMixIn (HashMap .class , HashMapMixin .class );
111
-
112
- return objectMapper ;
113
124
}
114
125
115
126
@ Override
0 commit comments