jackson-antpathfilter:无法解析ID为“ antPathFilter”的PropertyFilter

时间:2018-10-12 09:00:15

标签: spring-boot jackson

使用基于Spring 5.0.8.RELEASE的Spring Boot 2.0.4.RELEASE。

通过扩展DelegatingWebMvcConfiguration来配置AntPathFilter:

@Configuration
public class WebConfig extends DelegatingWebMvcConfiguration {
    @Override
    public void configureMessageConverters(final List<HttpMessageConverter<?>> messageConverters) {
        // Add a MappingJackson2HttpMessageConverter so that
        // objectMapper.writeFiltered
        // is using the objectMapper configured with the needed Mixin
        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder
            .json()
            .mixIn(Object.class, AntPathFilterMixin.class)
            .build();
        messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper));

        addDefaultHttpMessageConverters(messageConverters);
    }
}

此配置为那些应用AntPathFilter的控制器带来了问题。在这种情况下,会发生以下异常:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot resolve PropertyFilter with id 'antPathFilter'; no FilterProvider configured
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.findPropertyFilter(StdSerializer.java:426) ~[jackson-databind-2.9.6.jar:2.9.6]
...

(问题有点类似于@JsonFilter throws "JsonMappingException: Can not resolve BeanPropertyFilter"。)

0 个答案:

没有答案