Java 如何使用Spring Data Mongo DB为类建模,以存储具有不同字段长度和类型的嵌套JSON文档
我是Spring Boot和Mongo DB的新手,我正在尝试建模POJO类-“Patient”和“Survey”,它们应该存储和持久化患者调查数据。调查数据将作为JSON从第三方提供给我们,其中包含任意数量的问题和答案以及任意类型 我应该使用什么数据类型/注释将嵌套的JSON存储为单个实体/对象? 我听说MongoDB将JSON存储为BSON。但是我该怎么做呢 目前,我的模型类如下所示Java 如何使用Spring Data Mongo DB为类建模,以存储具有不同字段长度和类型的嵌套JSON文档,java,json,mongodb,spring-data-mongodb,bson,Java,Json,Mongodb,Spring Data Mongodb,Bson,我是Spring Boot和Mongo DB的新手,我正在尝试建模POJO类-“Patient”和“Survey”,它们应该存储和持久化患者调查数据。调查数据将作为JSON从第三方提供给我们,其中包含任意数量的问题和答案以及任意类型 我应该使用什么数据类型/注释将嵌套的JSON存储为单个实体/对象? 我听说MongoDB将JSON存储为BSON。但是我该怎么做呢 目前,我的模型类如下所示 @Document public class Patient { @Id private Stri
@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);
}
我得到的例外是
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的数据类型更改为“Object”,现在我可以将嵌入的JSON对象存储在Java模型类中
“Document”作为一种类型似乎无效,因为它是一种注释。通过将surveyData的数据类型更改为“Object”,现在我可以将嵌入的JSON对象存储在Java模型类中
似乎“Document”作为类型无效,因为它是注释。“…将JSON存储为GSON”-您可能指的是BSON,而不是GSON--GSON是一个库,而BSON是一种数据格式。这个问题似乎超出了gson的标记范围。你能提供一个调查的示例文档吗?嗨,Markus,是的,我指的是BSON“…将JSON存储为gson”-你可能指的是BSON,而不是gson--gson是一个库,而BSON是一种数据格式。这个问题似乎超出了gson的标签范围。你能提供一份调查的样本文档吗?嗨,Markus,是的,我是说BSON
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]