com.fasterxml.jackson.databind.JsonMappingException:无法解析id为'filterProfile'的PropertyFilter;没有配置FilterProvider

时间:2017-10-25 13:21:04

标签: json jersey

我已经对我的POJO(个人资料)对象进行了动态过滤。过滤逻辑工作正常。需要将过滤后的对象作为响应返回。但是在返回配置文件对象时,在jersey图层面临问题(MappableException)。

有人可以指导我。

过滤器逻辑代码:

    ObjectMapper mapper = new ObjectMapper(); 
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    FilterProvider filters = new SimpleFilterProvider()  .addFilter("filterProfile",SimpleBeanPropertyFilter.serializeAllExcept(set1))
            .addFilter("filterUserDetails", SimpleBeanPropertyFilter.serializeAllExcept(set2)).setFailOnUnknownId(false);  

    ObjectWriter writer = mapper.writer(filters);
    try{            
       // Printing the filtered json object
        filteredJson = writer.writeValueAsString(profile);
    }

POJO:

@JsonFilter( “filterProfile”)

public class Profile {  

    @JsonProperty("session")
    private Session session;

错误:

  

引起:com.fasterxml.jackson.databind.JsonMappingException:可以   不解析ID为'filterProfile'的PropertyFilter;没有FilterProvider   配置为   com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:312)     在   com.fasterxml.jackson.databind.ser.std.StdSerializer.findPropertyFilter(StdSerializer.java:441)     在   com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFieldsFiltered(BeanSerializerBase.java:731)     在   com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:153)     在   com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)     在   com.fasterxml.jackson.databind.ObjectWriter $ Prefetch.serialize(ObjectWriter.java:1429)     在   com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:951)     在   com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:625)     在   org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)     在   org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)     在   org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)     在   org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)     在   org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)

2 个答案:

答案 0 :(得分:0)

您的代码有意过滤序列化的属性,所以即使它没有抛出任何异常,过滤器也不会起作用,因为它缺少这一行:

mapper.addMixIn(Object.class, Profile.class);

然而,你说"过滤逻辑工作正常"。考虑到这一点, 请发布一个重现问题的最低工作代码(也许是一个主要方法?)。

答案 1 :(得分:0)

下面的代码片段对我来说很好

ObjectMapper mapper = new ObjectMapper(); 
         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept(set1);
        FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter).setFailOnUnknownId(false);
        ObjectWriter writer = mapper.writer(filterProvider);
        String writeValueAsString = writer.writeValueAsString(profile);
        Profile resultProfile = mapper.readValue(writeValueAsString,Profile.class);

注意:如果您没有将 stringformat json 转换为 Profile.As

{\"name\":\"JPA in Use\",\"reviews\":[{\"id\...