Skip to content

Commit ff90e4e

Browse files
committed
Optimize the usage of JacksonMongoSessionConverter to prevent duplicate MongoSession Document saves when a custom ObjectMapper is provided.#3185
1 parent ac32149 commit ff90e4e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

spring-session-data-mongodb/src/main/java/org/springframework/session/data/mongo/JacksonMongoSessionConverter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444
import org.springframework.security.jackson2.SecurityJackson2Modules;
4545
import org.springframework.session.FindByIndexNameSessionRepository;
4646
import org.springframework.util.Assert;
47+
import org.springframework.util.ClassUtils;
4748

4849
/**
4950
* {@code AbstractMongoSessionConverter} implementation using Jackson.
5051
*
5152
* @author Jakub Kubrynski
5253
* @author Greg Turnquist
5354
* @author Michael Ruf
55+
* @author TiQuan Hu
5456
* @since 1.2
5557
*/
5658
public class JacksonMongoSessionConverter extends AbstractMongoSessionConverter {
@@ -93,9 +95,12 @@ protected Query getQueryForIndex(String indexName, Object indexValue) {
9395
}
9496

9597
private ObjectMapper buildObjectMapper() {
96-
9798
ObjectMapper objectMapper = new ObjectMapper();
99+
configureObjectMapper(objectMapper);
100+
return objectMapper;
101+
}
98102

103+
public static void configureObjectMapper(ObjectMapper objectMapper) {
99104
// serialize fields instead of properties
100105
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
101106
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
@@ -105,11 +110,9 @@ private ObjectMapper buildObjectMapper() {
105110

106111
objectMapper.setPropertyNamingStrategy(new MongoIdNamingStrategy());
107112

108-
objectMapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
113+
objectMapper.registerModules(SecurityJackson2Modules.getModules(ClassUtils.getDefaultClassLoader()));
109114
objectMapper.addMixIn(MongoSession.class, MongoSessionMixin.class);
110115
objectMapper.addMixIn(HashMap.class, HashMapMixin.class);
111-
112-
return objectMapper;
113116
}
114117

115118
@Override

0 commit comments

Comments
 (0)