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字段
mongo不止查询时会entity和mongo库中数据映射,插入也会映射项目中insert list时entity的id为null报错,mongo中存入entity,即属性为_class字段为 com.tk.mer.entity.MerSku