java - 如何使用 Spring Data Mongo DB 对类进行建模来存储具有不同长度字段和类型的嵌套 JSON 文档

标签 java json mongodb spring-data-mongodb bson

我是 Spring Boot 和 Mongo DB 的新手,我正在尝试对 POJO 类进行建模 - “患者”和“调查”,它们应该存储和保留患者调查数据。调查数据将以 JSON 形式从第 3 方提供给我们,其中包含任意编号。问题和答案以及任意类型。

我应该使用什么数据类型/注释将嵌套 JSON 存储为单个实体/对象? 我听说 Mongo DB 将 JSON 存储为 BSON。但我该怎么做呢?

目前我的模型类看起来像这样

@Document
public class Patient {

    @Id private String id;


    private String pID;
    private String firstName;
    private String lastName;

    @DBRef
    private List<Survey> surveys;

    public Patient() { }

    public Patient(String fName, String lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public String getpID() {
        return pID;
    }

    public void setpID(String pID) {
        this.pID = pID;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}



@Document
public class Survey {

    @Id private String id;

    private String pID;

    private Document surveyData;

    public Survey(String pID, Document surveyData)
    {
        this.pID = pID;
        this.surveyData = surveyData;
    }

    public String getpID() {
        return pID;
    }

    public void setpID(String pID) {
        this.pID = pID;
    }

    public Document getSurveyData() {
        return surveyData;
    }

    public void setSurveyData(Document surveyData) {
        this.surveyData = surveyData;
    }

}

@RepositoryRestResource(collectionResourceRel = "Survey", path = "survey")
public interface SurveyRepository extends MongoRepository<Survey, String> {

    public Survey findBypID(@Param("pID") String pID);

}

这是我用来测试它的 JSON。

{
"pID": "test1",
"surveyData" : {
  "resourceType": "QuestionnaireResponse",
  "questionnaire": {
    "reference": "http://hl7.org/fhir/2016Sep/cqif/questionnaire-cqif-example.xml.html"
  },
  "item": [
    {
      "linkId": "Q1",
      "text": "Little interest or pleasure in doing things",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    },
    {
      "linkId": "Q2",
      "text": "Feeling down, depressed, or hopeless",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    },
    {
      "linkId": "Q3",
      "text": "Trouble falling or staying asleep",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    },
    {
      "linkId": "Q4",
      "text": "Feeling tired or having little energy",
      "answer": [
        {
          "valueString": "More than half days"
        }
      ]
    },
    {
      "linkId": "Q5",
      "text": "Poor appetite or overeating",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    },
    {
      "linkId": "Q6",
      "text": "Feeling bad about yourself - or that you are a failure or have let yourself or your family      down",
      "answer": [
        {
          "valueString": "More than half days"
        }
      ]
    },
    {
      "linkId": "Q7",
      "text": "Trouble concentrating on things, such as reading the newspaper or watching television",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    },
    {
      "linkId": "Q8",
      "text": "Moving or speaking so slowly that other people could have noticed. Or the opposite - being      so fidgety or restless that you have been moving around a lot more than usual",
      "answer": [
        {
          "valueString": "More than half days"
        }
      ]
    },
    {
      "linkId": "Q9",
      "text": "If you checked off any problems, how difficult have these problems made it for you to      do your work, take care of things at home, or get along with other people",
      "answer": [
        {
          "valueString": "Several Days"
        }
      ]
    }
  ]
}

}

我遇到的异常(exception)是

2017-03-15 17:18:19.257 ERROR 15364 --- [nio-8080-exec-3] o.s.d.r.w.RepositoryRestExceptionHandler : Could not read document: Can not deserialize Class org.springframework.data.mongodb.core.mapping.Document (of type annotation) as a Bean
 at [Source: org.apache.catalina.connector.CoyoteInputStream@5d3981f6; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize Class org.springframework.data.mongodb.core.mapping.Document (of type annotation) as a Bean
 at [Source: org.apache.catalina.connector.CoyoteInputStream@5d3981f6; line: 1, column: 1]

最佳答案

通过将 SurveyData 的数据类型更改为“对象”,现在我可以将嵌入的 JSON 对象存储在 Java 模型类中。

似乎“文档”作为一种类型无效,因为它是一个注释。

关于java - 如何使用 Spring Data Mongo DB 对类进行建模来存储具有不同长度字段和类型的嵌套 JSON 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42801581/

相关文章:

json数据中可以写注释吗?

mongodb - 使用mgo在mongo中删除所有早于使用_id的日期的文档

java - 创建原型(prototype)的副本,我应该使用克隆吗?

Java/MongoDB - 如何解决错误 "exception: pipeline element 0 is not an object"

java - 两个对象的 Spring 表单验证

java - Maven 库在更多具有不同依赖项的项目中使用

json - Circe 和 Scala 的枚举类型

java - 在android中使用简单的 TextView 和按钮小部件

java - Hibernate <set> 来自连接表的键

python - 在Python中大规模连接字符串