如何在 Spring Boot ObjectMapper 中排除带有自定义注释的字段

How can I exclude fields with custom annotation in Spring Boot ObjectMapper

我需要在应用程序中有两个不同的ObjectMapper

我正在使用的 Pojo:

public class Student {

 private String name;
 private Integer age;

 @HideThisField
 private String grade; 

 // getters & setters..
}

一个是基于 ObjectMapper 的开箱即用配置,如下所示:

@Bean("objectMapper")
public ObjectMapper getRegularObjectMapper() {
     //With some configurations 
     return new ObjectMapper();
}

我需要另一个 ObjectMapper,它在序列化时根据字段上的注释忽略所有对象的一些字段。

@Bean("customObjectMapper")
public ObjectMapper getCustomObjectMapper() {
   // This is where i want to ignore the fields with @HideThisField
   return new ObjectMapper();
}

两个映射器的输出:

objectMapper.writeValuesAsString(someStudent) 打印:

{"name": ""student1", age: 10, "grade": "A+"}

customObjectMapper.writeValuesAsString(someStudent) 打印:

{"name": ""student1", age: 10}

JacksonAnnotationIntrospector 处理标准 Jackson 注释。重写 hasIgnoreMarker 方法,你可以让它按照你自己的注解工作。

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.*;

import java.lang.annotation.*;

public class StudentExample {

    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setName("Student 1");
        student.setAge(10);
        student.setGrade("A+");

        String st1 = getRegularObjectMapper().writeValueAsString(student);
        String st2 = getCustomObjectMapper().writeValueAsString(student);

        System.out.println(st1);
        System.out.println(st2);
    }

    public static ObjectMapper getRegularObjectMapper() {
        return new ObjectMapper();
    }

    public static ObjectMapper getCustomObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
            @Override
            public boolean hasIgnoreMarker(AnnotatedMember m) {
                if (_findAnnotation(m, HideThisField.class) != null)
                    return true;
                return false;
            }
        });
        return objectMapper;
    }
}

class Student {

    private String name;
    private Integer age;

    @HideThisField
    private String grade;

    public Student() {
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface HideThisField {}

控制台输出为:

{"name":"Student 1","age":10,"grade":"A+"}
{"name":"Student 1","age":10}

getCustomObjectMapper() 不跳过 JsonIgnore 注释,因为你覆盖了标准,如果你想,你需要将它添加到 if 块中。