如何映射Mixins以嵌套JSON响应

时间:2014-02-13 10:33:09

标签: java json jackson jackson-modules

我使用Jackson API将 JSON响应映射到java对象。 例如,

回复{name:'karthikeyan',年龄:'24',性别:'男'}

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

是Mix-in,它运行正常。(内部我们将映射此pojo和Mix-in)。现在我如何在混合中表示以下响应?

{
name :'karthikeyan',
age:'24',
gender:'Male',
interest:
      {
        books:'xxx',
        music:'yyy',
        movie:'zzz'
      }
}

我试过以下,但没有运气。

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

@JsonProperty("interest")
public InterestPojo interestPojo;  //created same format mix-in and pojo for interest params as well.

但无法准确映射它们,请提出您的意见和想法如何做到这一点?

1 个答案:

答案 0 :(得分:7)

我尝试了以下内容:

    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());