0
我想插入一個json對象數組到mongodb中。我通過一個POST
請求陣列,使用Spring
InvalidDataAccessApiUsageException:沒有爲該類找到持久性實體信息com.mongodb.BasicDBList
我的目標
@Document(collection = "Users")
public class User {
private String name;
private String number;
//constructors, getters, setters
}
我的春天控制器
@RestController
public class UserController {
@RequestMapping(value="/postUser", method = RequestMethod.POST)
public void postUser(@RequestBody BasicDBList users){
ApplicationContext ctx =
new AnnotationConfigApplicationContext(SpringMongoConfig.class);
MongoOperations mongoOperation =
(MongoOperations) ctx.getBean("mongoTemplate");
mongoOperation.insert(users);
}
}
這是我的JSON
[
{
"name" : "a",
"number" : "1"
},
{
"name" : "c",
"number" : "3"
}
]
我得到的回報是什麼是
{
"timestamp": 1499161260902,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "No Persistent Entity information found for the class com.mongodb.BasicDBList",
"path": "/postUser"
}
是沒有問題的,如果我做
public void postUser(@RequestBody User users)
,並插入一個用戶。爲什麼它不起作用?
沒有幫助。新錯誤是'com.mongodb.BasicDBObject不能轉換爲com.mongodb.BasicDBList' – Evgenii
糾正。我還必須將'mongoOperation.insert(用戶)'改爲'insertAll(users)'。現在它可以工作。 – Evgenii