前陣子在使用 mongo 查詢時(shí)碰到了一個(gè)很神奇的報(bào)錯信息乍赫,而且該報(bào)錯信息僅在服務(wù)器啟動后第一次查詢時(shí)會產(chǎn)生,后續(xù)繼續(xù)使用就又正常了。針對該問題排查許久未果钾怔。最近又重新斷點(diǎn)排查了一遍却桶,終于發(fā)現(xiàn)了具體報(bào)錯的原因境输。
其實(shí)報(bào)錯原因很簡單,就是在使用注解定義索引時(shí)使用了中文的冒號(:)肾扰,底層代碼無法解析該冒號畴嘶,所以就報(bào)錯了。
我們項(xiàng)目的服務(wù)器啟動后的首次查詢時(shí)集晚,會遍歷代碼中注解的索引窗悯,如果該索引在 mongo 中不存在,則為 mongo 創(chuàng)建索引偷拔,已存在則不再處理蒋院。因此造成的現(xiàn)象就是每次服務(wù)器啟動后,第一次查詢時(shí)莲绰,總會報(bào)出題目中的錯誤欺旧。
如果有碰到類似問題的小伙伴,可以往這個(gè)方向排查看看蛤签。
大概的堆棧如下:
org.bson.json.JsonParseException: Invalid JSON input. Position: 16. Character: ':'.
at org.bson.json.JsonScanner.nextToken(JsonScanner.java:99)
at org.bson.json.JsonReader.popToken(JsonReader.java:492)
at org.bson.json.JsonReader.readBsonType(JsonReader.java:130)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)
at org.bson.Document.parse(Document.java:105)
at org.bson.Document.parse(Document.java:90)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveCompoundIndexKeyFromStringDefinition(MongoPersistentEntityIndexResolver.java:376)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinition(MongoPersistentEntityIndexResolver.java:345)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.lambda$createCompoundIndexDefinitions$2(MongoPersistentEntityIndexResolver.java:327)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.createCompoundIndexDefinitions(MongoPersistentEntityIndexResolver.java:328)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.potentiallyCreateCompoundIndexDefinitions(MongoPersistentEntityIndexResolver.java:211)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexForEntity(MongoPersistentEntityIndexResolver.java:109)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.resolveIndexFor(MongoPersistentEntityIndexResolver.java:90)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:134)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:127)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:111)
at com.exe.mongo.client.factory.MongoMappingEventPublisher.publish(MongoMappingEventPublisher.java:30)
at com.exe.mongo.client.factory.MongoMappingEventPublisher.publishEvent(MongoMappingEventPublisher.java:18)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:420)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:229)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:172)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:85)
at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:70)