如何创建能够支持@JsonFilter注释的POJO的全局过滤器

时间:2018-08-06 14:16:47

标签: java spring-boot jackson

在使用SimpleFilterProvider

创建简单过滤器时,我遇到了问题
Can not resolve PropertyFilter with id 'customerFilter'; no FilterProvider configured

我的代码是:

SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().setFailOnUnknownId(false);
FilterProvider filters =simpleFilterProvider.addFilter("customerFilter", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames)); 
ObjectWriter writer=null;
ObjectMapper mapper = new ObjectMapper();
mapper.setFilterProvider(filters);
writer=mapper.writer();
String dtoAsString = writer.writeValueAsString(customer);

我的POJO是客户

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

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonFilter("customerFilter") 
public class Customer {

    public Customer() {

    }

    public Customer(String customerId,String phoneNumber) {

        this.customerId=customerId;
        this.phoneNumber=phoneNumber;
    }

    /**
     * customer identifier
     */
    @JsonPropertyDescription("customer identifier")
    @JsonProperty("customerId")
    private String customerId;

    /**
     * customer phone number
     */
    @JsonPropertyDescription("customer phone number")
    @JsonProperty("phoneNumber")
    private String phoneNumber;
....

任何帮助请考虑我的应用程序是SpringBoot应用程序!

1 个答案:

答案 0 :(得分:1)

如果您手动(完全按照您的编码方式)序列化Customer对象(例如,在RestController中),则可以正常工作(请参见下面的方法getManual)。但是,依靠Spring对象映射器需要注册其他过滤器提供程序(没有它,方法getAuto将失败):

@RestController
@RequestMapping("/customer")
public class CustomerController {

    private static final Set<String> ignorableFieldNames= new HashSet<>();

    static {
        ignorableFieldNames.add("customerId");
    }

    @RequestMapping("auto")
    public Customer getAuto() throws JsonProcessingException {
        return new Customer("123", "456");
    }

    @RequestMapping("manual")
    public String getManual() throws JsonProcessingException {
        SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().setFailOnUnknownId(false);
        FilterProvider filters = simpleFilterProvider.addFilter("customerFilter", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));

        ObjectWriter writer = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setFilterProvider(filters);
        writer = mapper.writer();

        Customer customer = new Customer("123", "456");
        String dtoAsString = writer.writeValueAsString(customer);
        return dtoAsString;
    }
}

注册自定义过滤器:

@Component
public class CustomerFilterConfig {
    private static final Set<String> ignorableFieldNames = new HashSet<>();

    static {
        ignorableFieldNames.add("phoneNumber");
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();

        SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().setFailOnUnknownId(false);
        FilterProvider filters = simpleFilterProvider.addFilter("customerFilter", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
        objectMapper.setFilterProvider(filters);

        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }    
}