web项目整合MongoDb出现No converter found capable of converting from org.bson.types.ObjectId to type Long异常


Spring、SpringMVC、MyBatis框架整合 MongoDb ,启动web项目时出现异常,项目不能运行,查看原因是出现如下异常:

with root cause org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.Long


异常堆栈如下

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [springMvcDispatcherServlet] in context with path [/conference] threw exception [Request processing failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.Long] with root cause
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.Long

解决方法:

Spring使用MongoDb帮助文档

在MongoDb里要求每个文档都需要有_id 字段,java类中有如下情况会被映射为_id字段

  • 如果1个字段加上了 @Id (org.springframework.data.annotation.Id)注解,那么将bean保存到数据库时就会把该字段映射为文档中的_id字段
  • 如果java对象中没有 @Id 注解,名字为id 的字段将会被映射为文档中的_id字段

web项目整合MongoDb出现No converter found capable of converting from org.bson.types.ObjectId to type Long异常




web项目整合MongoDb出现No converter found capable of converting from org.bson.types.ObjectId to type Long异常





web项目整合MongoDb出现No converter found capable of converting from org.bson.types.ObjectId to type Long异常