2017-04-26 11 views
0

websocket経由でバックエンドに送信するJSONスキーマがあります。DataIntegrityViolationException:Spring mongodbでJSONスキーマを保存できません

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

このための私のBeanクラスは、今私は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] 

の下に取得することを実行しようとするとき私は

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

として定義されます$ refはmongodbで使用される有効なキー名ではありませんが、Open API仕様に基づいたJSONスキーマの有効なキーであることを推測しています。 これには解決策はありますか?

答えて

0

文字列として送信するwebsocketを通じてデータを送信していたので、$refをmongodbの有効なキー名である文字列(たとえば##ref##)に置き換えます。

strData = strData.replace(new RegExp('"\\$ref":', 'g'), '"##ref##":'); 
self.stomp.send('/app/execute', {}, strData); 

そして、私はバックエンドからそれを受信したときに、私は再び$ref##ref##を交換してください。

body = body.replace(new RegExp('"##ref##":', 'g'), '"$ref":'); 
1

this mongodb JIRAでは、.を含むか、$で始まるキーは保存できません。したがって、ここでの唯一の解決策は、オブジェクトをmondogbに保存する前に$を手動でエスケープし、取得中に\\を削除することです。

これらのオブジェクトの格納/取得を扱うレイヤーにこのロジックを書き込むことができます。

関連する問題