【发布时间】:2016-05-20 06:42:45
【问题描述】:
我在我的 POJO 类中使用 @JsonAnySetter 和 @JsonAnyGetter,使用我的自定义序列化与 DSL JSON 类,映射已初始化,但其他属性始终为空。 我的 POJO 课:
@CompiledJson
public class Name {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Map<String,String> properties = new HashMap<String,String>();
public Name() {
// TODO Auto-generated constructor stub
}
@JsonAnyGetter
public Map<String, String> get() {
return this.properties;
}
@JsonAnySetter
public void set(String key, String value) {
this.properties.put(key, value);
}
使用 DSLJson serialize() 和 deserialize() 方法进行反序列化。我也没有看到任何错误,但 JSON 中的属性仍然为空。我怀疑 DSL Json 是否支持 Jackson 注释。 :/
带有 DSL Json 和 Jackson 注释的 Spring Boot 应用
更新 我要解析MyClass,它是RootClass的一部分:
@Compiledjson
public class RootClass {
private String id;
private List<MyClass> myclass;
private AnotherCLass class2;
//getters and setter here
}
@CompiledJson
public class MyClass implements JsonObject {
private String name;
private Map<String, String> properties; //want this to behave like Jackson's @JsonAnySetter/Getter annotation.
//The implementation of MapConverter serializer you mentioned below.
}
整个代码通过自定义消息读取器和写入器进行解析。
在发送我的 JSON 正文时,它会是这样的:
{
"id" : "1234",
"myclass" :
[
{
"name" : "abcd",
//any dynamic properties I want to add will go here
"test" : "test1",
"anything" : "anything"
}
],
"class2" : "test5"
}
谢谢你:)
【问题讨论】:
标签: json annotations jackson dsl