jackson 如何在Spring Boot ObjectMapper中排除具有自定义注解的字段

dgiusagp  于 2022-11-09  发布在  Spring
关注(0)|答案(1)|浏览(164)

我需要在应用程序中有两个不同的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();
}

两个Map器的输出:
objectMapper.writeValuesAsString(someStudent)打印:
{【姓名】:【学生1】,年龄:10、“等级”:A +
customObjectMapper.writeValuesAsString(someStudent)打印:
{【姓名】:【学生1】,年龄:10个月}

jtoj6r0c

jtoj6r0c1#

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块中。*

相关问题