被用来更新到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