gpt4 book ai didi

java - 数据完整性违规异常 : Unable to store JSON Schema with spring mongodb

转载 作者:行者123 更新时间:2023-12-02 13:19:23 34 4
gpt4 key购买 nike

我有一个如下所示的 JSON 架构,我将通过 websocket 将其发送到后端。

{
"type": "object",
"properties": {
"user": {
"$ref": "#/definitions/user"
}
},
"required": [
"user"
]
}

我的 bean 类定义为

class Schema{
private String type;
private String[] required;
Private Map<String, Object> properties;
//getter and setter
}

现在我想将其存储在 mongodb 中,但是当我尝试这样做时,我遇到了异常

org.springframework.dao.DataIntegrityViolationException: Write failed with error code 55 and error message 'The DBRef $ref field must be following by a $id field'; nested exception is com.mongodb.WriteConcernException: Write failed with error code 55 and error message 'The DBRef $ref field must be following by a $id field'
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:85) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2135) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:481) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:1101) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1034) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:981) ~[spring-data-mongodb-1.10.1.RELEASE.jar:na]

我猜测 $ref 不是 mongodb 中使用的有效键名称,但它是基于开放 API 规范的 JSON 模式的有效键,我想按原样撕掉它。有什么解决办法吗?

最佳答案

根据 this mongodb JIRA,您无法保存包含 . 或以 $ 开头的 key 。因此,我相信这里唯一的解决方案是在将对象存储到 mondogb 之前手动转义 $ 并在检索它时删除 \\

您可以在处理这些对象的存储/检索的层中编写此逻辑。

关于java - 数据完整性违规异常 : Unable to store JSON Schema with spring mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43627444/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com