-
-
Save superlucky8848/20179411878a57fb507490e4239c6c9b to your computer and use it in GitHub Desktop.
Spring - MongoDB without _class field
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
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.ImportResource; | |
import org.springframework.data.mongodb.MongoDbFactory; | |
import org.springframework.data.mongodb.core.MongoTemplate; | |
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; | |
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper; | |
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; | |
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; | |
@Configuration | |
public class MongoConfig { | |
/** | |
* Removes the _class field for all registers within mongodb | |
*/ | |
@Bean | |
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, | |
MongoMappingContext context) { | |
MappingMongoConverter converter = | |
new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory), context); | |
converter.setTypeMapper(new DefaultMongoTypeMapper(null)); | |
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter); | |
return mongoTemplate; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment