Spring boot에서 MongoDB 저장 시 _class…
Spring boot에서 MongoDB에 데이터를 저장하였고, 이를 확인하다가 이상한 필드를 발견하였다.
1 2 3 4 5 6 7 8 9
| { "_id":"5c627b7ec67c652002bf3601", "createDate":"2019-02-12T07:53:33.976Z", "accessIp":"192.168.0.1", "logCode":"SAMPLE_CODE", "msg":"", "accessUser":"Test user", "_class":"net.movill.movill.repo.mongo.collections.LogData" }
|
마지막 열에 _class 라는 필드였다.
사실 이 필드는 필요도 없고 굳이 기록할 필요는 없을 것 같아서 이 필드가 저장되지 않게끔 처리하기 위한 방법을 찾아봤다.
Spring boot에 MongoDB를 연동하는 부분은 다음에 포스팅에 소개할 예정이다.
How to?
프로젝트 환경이 각각 다를 수 있지만, 내가 진행한 프로젝트는 Java 설정 기반으로 진행하였고, 이를 토대로 설명할 예정이다.
아래의 코드를 참고하자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.convert.DbRefResolver; 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 MongoDBConfiguration { @Autowired private MongoDbFactory mongoDbFactory; @Autowired private MongoMappingContext mongoMappingContext;
@Bean public MappingMongoConverter mappingMongoConverter() { DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory); MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); return converter; } }
|
저 코드에서 converter 부분의 new DefaultMongoTypeMapper(null) 부분을 설정해주면 _class 필드는 사라지게 된다.