【问题标题】:fasterxml jackson ObjectMapping set @JsonView class not workingfastxml jackson ObjectMapping 设置 @JsonView 类不起作用
【发布时间】:2015-03-03 14:34:26
【问题描述】:
MappingJackson2HttpMessageConverter objConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objMapper = new ObjectMapper();
objMapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE);
objMapper.getSerializationConfig().withView(View.class);
objConverter.setObjectMapper(objMapper);
objConverter.getObjectMapper().getSerializationConfig().withView(View.class);

之后

objMapper.getSerializationConfig()

具有与使用方法 (..).withView(..) 创建的 SerializationConfig 不同的引用

看起来好像新的SerializationConfig 没有应用于objMapper

解决这个问题很热门?我的 @JsonView 注释也不起作用。

fasterxml.jackson 2.3.0

【问题讨论】:

  • 从格式化中去掉标签,这在这里效果不佳。还反引号内联代码以增加代码的视觉识别。可读性增加了别人理解你的问题的机会,从而增加了答案。
  • 也许这个问题对你有帮助:stackoverflow.com/a/21054896/4576054

标签: java json spring jackson fasterxml


【解决方案1】:

基于Javier Molla的链接答案:

您应该在您的 ObjectMapper 上使用 .configure(),并且最好重复使用该映射器。

final ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
mapper.setSerializationInclusion(Include.NON_NULL);

您可以将序列化功能放在一个静态块中并重用映射器 => 性能提升。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2016-04-20
    • 2015-10-07
    • 1970-01-01
    • 2012-07-16
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多