我读过以下问题:
https://stackoverflow.com/a/25609465/2674303
我有spring-mvc应用程序.如果我使用@ResponseBody注释注释控制器方法并在方法内返回对象,则服务器将json提供给客户端.我有杰克逊在课堂上.
在提到的例子中写了以下代码
ObjectMapper mapper = new ObjectMapper();
mapper.setFilters(new SimpleFilterProvider().addFilter("filter", new ExcludeIdFilter()));
据我所知,这段代码已经写在春天的内部……
请说明如何注册自定义jackson过滤器?
最佳答案 您可以像这样配置自己的MappingJackson2HttpMessageConverter实例:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper mapper = Jackson2ObjectMapperBuilder().json()
.filters(new SimpleFilterProvider().addFilter("filter", new ExcludeIdFilter()));
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
// then replace the default MappingJackson2HttpMessageConverter
// with your custom one in the list of configured converters
}
}