@JSonIgnore 注释的等效代码设置是什么? [英] What is equivalent code settings for @JSonIgnore annotation?

查看:26
本文介绍了@JSonIgnore 注释的等效代码设置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉 JavaJackson 以及我尝试使用的许多其他技术,所以我希望得到详细的答复.

I'm new to Java and Jackson and a lot of other technologies which I try to use, so I'd appreciate a detailed answer.

有没有办法防止一个或多个字段使用 Jackson 序列化为 JSON String_like 格式,但不使用任何类型的 JSON注释?

Is there a way to prevent one or more fields from being serialized using Jackson into a JSON String_like format, but without using any kind of JSON annotations?

类似:mapper.getSerializationConfig().something(ignore("displayname")) 如果你明白我的意思.我的对象是一个类的实例,它扩展了另一个类,并实现了一个接口,依此类推,因此这些字段来自类的层次结构.我需要该对象的 JSON 表示,但只包含某些字段,因此我可以通过 POST 方法在模拟请求中发送该 JSON.我正在使用 Jackson 2.2.2.

Something like: mapper.getSerializationConfig().something(ignore("displayname")) if you know what I mean. My object is an instance of a class that extends another one, and implements one interface also so on, so the fields come from an hierarchy of classes. I need the JSON representation for that object but containing only certain fields, so I can send that JSON in a mock request through a POST method. I'm using Jackson 2.2.2.

推荐答案

如果你不能改变你的类,你可以创建新的 abstract class/interface with methods with@JsonIgnore 注释.在这个 class/interface 中,你可以定义在 serialization/deserialization 过程中 ObjectMapper 应该跳过的方法代码>过程.

If you can't change your classes you can create new abstract class/interface with methods with @JsonIgnore annotation. In this class/interface you can define methods which ObjectMapper should skip during serialization/deserialization process.

请看下面的例子:

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        Person person = new Person();
        person.setId(1L);
        person.setName("Max");

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.addMixIn(Person.class, PersonMixIn.class);

        System.out.println(objectMapper.writeValueAsString(person));
    }
}

abstract class Entity {

    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

interface Namamble {
    String getName();
}

class Person extends Entity implements Namamble {

    private String name;

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

interface PersonMixIn {
    @JsonIgnore
    String getName();
}

编辑 - 回答评论

你可以创建这样的mixin接口:

You can create such mixin interface:

public static interface UserInformationMixIn {
    @JsonIgnore
    String getField3();
}

并以这种方式配置ObjectMapper:

objectMapper.addMixInAnnotations(UserInformation.class, UserInformationMixIn.class);

在版本 2.5 方法 addMixInAnnotations 被弃用,addMixIn 应该使用:

In version 2.5 method addMixInAnnotations was deprecated and addMixIn should be used:

objectMapper.addMixIn(UserInformation.class, UserInformationMixIn.class);

完整示例源代码:

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        UserInformation userInformation = new UserInformation();
        userInformation.setField3("field3");
        userInformation.setField4("field4");
        userInformation.setField5("field5");

        User user = new User();
        user.setField1(userInformation);
        user.setField2("field2");

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.addMixIn(UserInformation.class, UserInformationMixIn.class);
        objectMapper.addMixIn(User.class, UserInformationMixIn.class);

        System.out.println(objectMapper.writeValueAsString(user));
    }

    public static abstract class Someclass {
        String field5;

        public String getField5() {
            return field5;
        }

        public void setField5(String field5) {
            this.field5 = field5;
        }
    }

    public static class UserInformation extends Someclass {
        String field3;
        String field4;

        public String getField3() {
            return field3;
        }

        public void setField3(String field3) {
            this.field3 = field3;
        }

        public String getField4() {
            return field4;
        }

        public void setField4(String field4) {
            this.field4 = field4;
        }
    }

    public static class User {
        UserInformation field1;
        String field2;

        public UserInformation getField1() {
            return field1;
        }

        public void setField1(UserInformation field1) {
            this.field1 = field1;
        }

        public String getField2() {
            return field2;
        }

        public void setField2(String field2) {
            this.field2 = field2;
        }
    }

public static interface UserInformationMixIn {
    @JsonIgnore
    String getField3();

    @JsonIgnore
    String getField2();

    @JsonIgnore
    String getField5();
}
}

有用的链接:

这篇关于@JSonIgnore 注释的等效代码设置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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