Jackson 動態過濾屬性,編程式過濾對象中的屬性


場景:有時候我們做系統的時候,比如兩個請求,返回同一個對象,但是需要的返回字段並不相同。

常見與寫前端接口的時候,尤其是手機端,一般需要什么數據就返回什么樣的數據。此時對於返回同一個對象我們就要動態過濾所需要的字段...

Spring MVC 默認使用轉json框架是 jackson。 大家也知道, jackson 可以在實體類內加注解,來指定序列化規則,但是那樣比較不靈活,不能實現我們目前想要達到的這種情況

下面用編程式的方式實現過濾字段.

先寫個json工具類:

 

public class JsonUtilJackson {

    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final ObjectMapper mapper;

    JacksonJsonFilter jacksonFilter = new JacksonJsonFilter();

    static {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        mapper = new ObjectMapper();
        mapper.setDateFormat(dateFormat);
        // 允許對象忽略json中不存在的屬性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 允許出現特殊字符和轉義符
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        // 允許出現單引號
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        // 忽視為空的屬性
        mapper.setSerializationInclusion(Include.NON_EMPTY);
    }

    public static JsonUtilJackson me() {
        JsonUtilJackson jsonUtil = new JsonUtilJackson();
        return jsonUtil;
    }

    public void filter(Class<?> clazz, String include, String filter) {
        if (clazz == null)
            return;
        if (StringUtils.isNotBlank(include)) {
            jacksonFilter.include(clazz, include.split(","));
        }
        if (StringUtils.isNotBlank(filter)) {
            jacksonFilter.filter(clazz, filter.split(","));
        }
        mapper.addMixIn(clazz, jacksonFilter.getClass());
    }

    public String toJson(Object obj) {
        try {
            mapper.setFilterProvider(jacksonFilter);
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException("轉換json字符失敗!");
        }
    }

    public <T> T toObject(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("將json字符轉換為對象時失敗!");
        }
    }

    public <T> T toObject(String json, TypeReference<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("將json字符轉換為對象時失敗!");

        }
    }

}

 

 

 

然后寫個JsonFilter

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;

@SuppressWarnings("deprecation")
@JsonFilter("JacksonFilter")
public class JacksonJsonFilter extends FilterProvider{

    Map<Class<?>, Set<String>> includeMap = new HashMap<>();
    Map<Class<?>, Set<String>> filterMap = new HashMap<>();

    public void include(Class<?> type, String[] fields) {
        addToMap(includeMap, type, fields);
    }

    public void filter(Class<?> type, String[] fields) {
        addToMap(filterMap, type, fields);
    }
    
    private void addToMap(Map<Class<?>, Set<String>> map, Class<?> type, String[] fields) {
        Set<String> filedSet=new HashSet<>();
        if(fields!=null && fields.length>0){
            for(String filed : fields){
                filedSet.add(filed);
            }
        }
        map.put(type, filedSet);
    }

    @Override
    public BeanPropertyFilter findFilter(Object filterId) {
        throw new UnsupportedOperationException("Access to deprecated filters not supported");
    }

    @Override
    public PropertyFilter findPropertyFilter(Object filterId, Object valueToFilter) {

        return new SimpleBeanPropertyFilter() {

            @Override
            public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer)
                    throws Exception {
                if (apply(pojo.getClass(), writer.getName())) {
                    writer.serializeAsField(pojo, jgen, prov);
                } else if (!jgen.canOmitFields()) {
                    writer.serializeAsOmittedField(pojo, jgen, prov);
                }
            }
        };
    }

    public boolean apply(Class<?> type, String name) {
        Set<String> includeFields = includeMap.get(type);
        Set<String> filterFields = filterMap.get(type);
        if (includeFields != null && includeFields.contains(name)) {
            return true;
        } else if (filterFields != null && !filterFields.contains(name)) {
            return true;
        } else if (includeFields == null && filterFields == null) {
            return true;
        }
        return false;
    }

}

最后就是我們測試了

 

    public static void main(String[] args) throws Exception {
        
        A a=new A();
        a.setName("AAAAA");
        a.setNo("011111");
        
        Role r=new Role();
        r.setName("zhangsan");
        r.setCode("11");
        r.setCreateTime(new Date().getTime());
        r.setRemark("who am i");

        a.setR(r);
        
        
        JsonUtilJackson jtk= new JsonUtilJackson();
        // 設置轉換 Article 類時,只包含 id, name
        jtk.filter(A.class, "no,r", null);  
        jtk.filter(Role.class, "name,remark", null);
        String ss=jtk.toJson(a);
        System.out.println(ss);
        
    }

結果:{"no":"011111","r":{"name":"zhangsan","remark":"who am i"}}

 

參考:https://my.oschina.net/diamondfsd/blog/836727?p=3&temp=1490578919756#blog-comments-list

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM