我尝试了以下方法:
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new Nested("books", "Music", "Movie"))));
public class Something {
@JsonProperty("name")
public String name;
@JsonProperty("age")
public int age;
@JsonProperty("gender")
public String gender;
@JsonProperty("interest")
public Nested nested;
//Constructor
}
public class Nested {
@JsonProperty("books")
public String books;
@JsonProperty("music")
public String music;
@JsonProperty("movie")
public String movie;
//Constructor
}
输出是:
{
"name":"Name",
"age":12,
"gender":"male",
"interest":
{
"books":"books",
"music":"Music",
"movie":"Movie"
}
}
所以一切都按预期工作。如果您提供一些 setter 和 getter 并将字段的可见性设置为私有,我已经检查过是否有区别,但这并没有什么不同。
也许您想向我们展示您的 InterestPojo 或您的输出/堆栈跟踪?
编辑:好的,我想我明白了;)
我尝试了以下方法:
public void start() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Nested.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Nested.class);
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new NestedImpl("name", null))));
}
public final class Something {
private final String name;
private int age;
private String gender;
// thats your interest thing
public Nested nested;
public Something(String name, int age, String gender, Nested nested) {
this.name = name;
this.age = age;
this.gender = gender;
this.nested = nested;
}
String getName() {
return name;
}
Nested getNested() {
return nested;
}
}
public abstract class Nested {
@JsonProperty("name-ext")
abstract String getName();
@JsonProperty("interest-ext")
abstract Nested getNested();
}
public class NestedImpl extends Nested {
private String name;
private Nested nested;
private NestedImpl(String name, Nested nested) {
this.name = name;
this.nested = nested;
}
@Override
String getName() {
return name;
}
@Override
Nested getNested() {
return nested;
}
}
输出:
{
"age":12,
"gender":"male",
"name-ext":"Name",
"interest-ext":
{
"name-ext":"name",
"interest-ext":null
}
}
那不完全是你的结构,但我认为这就是你想要的。我对吗?
EDIT2:我用 JSON->Object 和 Object->JSON 测试了以下结构。
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getSerializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
Nested nested = new Nested();
nested.setName("Nested");
nested.setNumber(12);
Something some = new Something();
some.setName("Something");
some.setAge(24);
some.setGender("Male");
some.setNested(nested);
String json = mapper.writeValueAsString(some);
System.out.println(json);
Something some2 = mapper.readValue(json, Something.class);
System.out.println("Object: " + some2);
public abstract class Mixin {
@JsonProperty("name")
private String _name;
@JsonProperty("age")
private int _age;
@JsonProperty("gender")
private String _gender;
@JsonProperty("interest")
private Nested nested;
}
public class Something {
private String _name;
private int _age;
private String _gender;
private Nested nested;
// You have to provide Setters and Getters!!
}
public abstract class NestedMixin {
@JsonProperty("nameNested")
private String name;
@JsonProperty("numberNested")
private int number;
}
public class Nested {
private String name;
private int number;
// You have to provide Setters and Getters!!
}
输出:
{"age":24,"gender":"Male","name":"Something","interest":{"nameNested":"Nested","numberNested":12}}
Object: Something{name=Something, age=24, gender=Male, nested=Nested{name=Nested, number=12}}
注意:杰克逊似乎在内部类方面遇到了问题。因此,如果您在一个额外的项目中测试这些示例,则创建额外的类文件;)
EDIT3:如果您使用的是模块,请尝试以下操作:
public class JacksonMixinModule extends SimpleModule {
public JacksonMixinModule() {
super("JacksonMixinModule", new Version(0, 1, 0, "SNAPSHOT"));
}
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(Something.class, Mixin.class);
context.setMixInAnnotations(Nested.class, NestedMixin.class);
}
}
...
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonMixinModule());