使用基于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"。)