被用來更新到1.3.0 jongo從我們的MongoDB閱讀文檔時開始出現以下錯誤:日期和「多態」類導致JsonGenerationException:BsonSerializer只能BsonGenerator
com.fasterxml.jackson.core.JsonGenerationException: BsonSerializer can only be used with BsonGenerator
經過一些測試我發現問題發生在使用@JsonTypeInfo
和MongoDB文檔包含 日期對象之前的類型屬性。鑑於:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXISTING_PROPERTY,
property = 'type',
visible = true)
@JsonSubTypes([
@JsonSubTypes.Type(name = 'a', value = A),
@JsonSubTypes.Type(name = 'b', value = B)
])
abstract class Base {
String string // For reference
Date date
String type
}
class A extends Base { A() { type = 'a' } }
class B extends Base { B() { type = 'b' } }
這(斯波克)會導致測試失敗
def mapper = new ObjectMapper(new BsonFactory()).registerModule(new BsonModule())
def bytes = mapper.writeValueAsBytes(original)
expect:
def parsed = mapper.readValue(bytes, Base)
parsed instanceof A // com.fasterxml.jackson.core.JsonGenerationException: BsonSerializer can only be used with BsonGenerator
parsed.string == original.string
parsed.date == original.date // parsed.date is null with 'de.undercouch:bson4jackson:2.8.0-SNAPSHOT'
parsed.type == original.type
where:
testCase | original
'A' | new A(string: 'string', date: new Date(), type: 'a') // fails
'String, Date, Type' | [string: 'string', date: new Date(), type: 'a'] // fails
'String, null date, Type' | [string: 'string', date: null, type: 'a']
'String, Type, Date' | [string: 'string', type: 'a', date: new Date()]
'Type, String, Date' | [type: 'a', string: 'string', date: new Date()]
注意,如果date
爲空或type
後自帶的測試通過。
我想更新Jongo和傑克遜,但不認爲我可以保證我們的數據庫上的屬性 的順序。問題是如果問題可以解決。
- 我試過更新傑克遜到2.8.6和2.8.7,但沒有區別。
- 當我嘗試
de.undercouch:bson4jackson:2.8.0-SNAPSHOT
解析date
將是無效的,如果涉及後type
類似的bug似乎已經解決了2.8.0-快照:https://github.com/michel-kraemer/bson4jackson/issues/67
我已經張貼問題這裏:https://github.com/michel-kraemer/bson4jackson/issues/72
您是否找到解決此問題的解決方案?我們面臨完全相同的問題,並使用de.undercouch:bson4jackson:2.8.0-SNAPSHOT無法解決它。 – user3495469