如何映射Mixins以嵌套JSON响应 [英] How to map the Mixins for the nested JSON response

查看:60
本文介绍了如何映射Mixins以嵌套JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Jackson APIs for Mapping my JSON response into a java object. For example,

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

for the response { name :'karthikeyan',age:'24',gender:'Male'}

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

是混合输入并且工作正常。(内部我们将映射此pojo和Mix- in)。现在我如何在混合输入中表示以下响应?

is the Mix-in and it works fine.(internally we will be mapping this pojo and Mix-in).Now how can i represent the following response in a Mix-in?

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

我试过以下但没有运气。

i have tried with the following, but no luck.

@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.

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

but unable to map them exactly, give your comments and thoughts on how to do it ?

推荐答案

我尝试了以下方法:

    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,并设置了私有字段的可见性,那是否有区别但是没有区别。

So everything is working as expected. I already checked if theres a difference if you provide some setters and getters and setting the visibility of the fields on private but that doas not make a difference.

也许你想要向我们展示您的InterestPojo或您的输出/堆栈跟踪?

Maybe you want to show us your InterestPojo or your output/stacktrace?

编辑:
好​​的我认为我得到了它;)

Okay i think i got it ;)

我尝试了以下内容:

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
    }
}

这不完全是你的结构,但我认为这就是你想要的。我是对的吗?

Thats not exactly your structure, but I think thats what you want. Am I right?

EDIT2:我使用JSON-> Object和Object-> JSON测试了以下结构。

I Tested the following structure with JSON->Object and 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, 性别: 男性, 姓名: 东西, 利息:{ nameNested: 嵌套, numberNested:12}}

对象:某事{name = Something,age = 24,gender = Male,nested = Nested {name = Nested,number = 12}}

注意:杰克森似乎遇到了内部课程的问题。因此,如果您在额外项目中测试该示例,请创建额外的类文件;)

Note: It seems that jackson got problems with inner classes. So if you test that examples in an extra project create extra class-files ;)

EDIT3:如果您使用的是模块,请尝试以下操作:

If you are using a Module, try the following:

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

这篇关于如何映射Mixins以嵌套JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆