概述
为什么80%的码农都做不了架构师?>>>
直接上代码
http://wenku.baidu.com/link?url=-Pcb0NwzGWhqHRz6lZGP6NRJonpR9kf8HoTcLYNjHn8vHuVvLQ7Ea1tz-Dsfd5p70r5eUGJXte_61Hgx8mMx-oJN0huQS5WA14ENiIR5qxC
根据文库中介绍的解决方案做的,
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; } }
转载于:https://my.oschina.net/u/860872/blog/510334
最后
以上就是顺利小蝴蝶为你收集整理的JackSon过滤字段直接上代码的全部内容,希望文章能够帮你解决JackSon过滤字段直接上代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复