3
回答
jackson 过滤字段
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

最近在用Jackson做JSON序列化时候遇到一个问题,怎么过滤不想要输出的字段呢?~~

POJO:

public class Foo{
  String foo;
  String bar;
  String foobar;
  //setter getter...
}
Foo foo = new Foo();
foo.setFoo("foo");
foo.setBar("bar");

ObjectMapper mapper = new ObjectMapper();
SerializationConfig cfg = mapper.getSerializationConfig();
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept({"bar"}));
cfg.withFilters(filterProvider);
mapper.setSerializationConfig(cfg);
String json = mapper.writeValueAsString(user);
L.print("str:" + json);//-->{"foo":"foo","bar":"bar","foobar":null}
//但是我想要的是:-->{"foo":"foo","foobar":null}
怎么办啊···不要告诉我用神马@jacksonIgnore 之类的注解啊····
<无标签>
举报
超能小猫
发帖于5年前 3回/6K+阅
共有3个答案 最后回答: 2年前

可以的使用的方式是:

package com.jizhi.test.type;


import java.util.ArrayList;
import java.util.List;


import org.junit.Test;


import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;


public class JsonTest {


@Test
public void filterTest() throws JsonProcessingException {
Person person = new Person();
person.set("炸了", true, 250);
ObjectMapper mapper = new ObjectMapper();
mapper.setFilters(filter("objectFilter", "name", "persons"));
mapper.addMixInAnnotations(Person.class, ObjectFilterMixIn.class);
System.out.println(mapper.writeValueAsString(person));
}


public FilterProvider filter(String filterName, String... propertyes) {
// 过滤不想要的
FilterProvider filter = new SimpleFilterProvider().addFilter(
filterName,
SimpleBeanPropertyFilter.serializeAllExcept(propertyes));
// 过滤想要的
/*
* FilterProvider filter = new SimpleFilterProvider().addFilter(
* filterName, SimpleBeanPropertyFilter.filterOutAllExcept(propertyes);
*/
return filter;
}
}


@JsonFilter("objectFilter")
interface ObjectFilterMixIn {
}


class Person {


private String name;


private boolean gender;
private int age;
private List<Person> persons = new ArrayList<Person>();


public void set(String name, boolean gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}


public String getName() {
return name;
}


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


public boolean isGender() {
return gender;
}


public void setGender(boolean gender) {
this.gender = gender;
}


public int getAge() {
return age;
}


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


public List<Person> getPersons() {
return persons;
}


public void setPersons(List<Person> persons) {
this.persons = persons;
}


}

在这里能看到解决方案

http://wenku.baidu.com/link?url=-Pcb0NwzGWhqHRz6lZGP6NRJonpR9kf8HoTcLYNjHn8vHuVvLQ7Ea1tz-Dsfd5p70r5eUGJXte_61Hgx8mMx-oJN0huQS5WA14ENiIR5qxC

--- 共有 1 条评论 ---
雨vs枫找的就是这个,谢谢!!! 1年前 回复
顶部