简体   繁体   English

InvalidDataAccessApiUsageException:没有为com.mongodb.BasicDBList类找到持久性实体信息

[英]InvalidDataAccessApiUsageException: No Persistent Entity information found for the class com.mongodb.BasicDBList

I am trying to insert an array of json objects into mongodb. 我正在尝试将json对象数组插入mongodb中。 I pass the array with a POST request, using Spring 我使用Spring通过POST请求传递数组

My object 我的对象

@Document(collection = "Users")
public class User {
  private String name;
  private String number;
//constructors, getters, setters
}

My spring controller 我的弹簧控制器

@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);
  }
}

This is my json 这是我的json

[
    {
        "name" : "a",
        "number" : "1"
    },
    {
        "name" : "c",
        "number" : "3"      
    }
]

What I get in return is 我得到的回报是

{
    "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"
}

There is no problem if I do 如果我没有问题

public void postUser(@RequestBody User users)

and insert a single user. 并插入一个用户。 Why doesn't it work? 为什么不起作用?

When you add @RequestBody before the parameter, HttpMessageConvertor will try to convert json string to the specified type - BasicDBList . 当在参数之前添加@RequestBodyHttpMessageConvertor将尝试将json字符串转换为指定的类型BasicDBList The json string may not match BasicDBList , so the conversion failed. json字符串可能与BasicDBList不匹配,因此转换失败。 You can use this: 您可以使用此:

public void postUser(@RequestBody List<User> users)

Actually, Spring data cannot determine the collection name to save BasicDBList. 实际上,Spring数据无法确定要保存BasicDBList的集合名称。
This is not related to HttpMessageConverter 这与HttpMessageConverter无关
You can check more here: Insert DBObject into MongoDB using Spring Data 您可以在此处查看更多信息: 使用Spring Data将DBObject插入MongoDB

Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: No Persitent Entity information found for the class com.mongodb.BasicDBObject at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:1747)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM