Jackson JsonView未被应用 [英] Jackson JsonView not being applied

查看:80
本文介绍了Jackson JsonView未被应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jackson 2.2.2

Jackson 2.2.2

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().withView(Views.Public.class);
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
// if I try to simply configure using .without that config feature doesn't get set.
// I MUST use the .configure as above, but I guess that's a different question.
//        .without(MapperFeature.DEFAULT_VIEW_INCLUSION);

// this first one doesn't have the view applied.
String result = mapper.writeValueAsString(savedD);

// this second one works.
result = mapper.writerWithView(Views.Public.class).writeValueAsString(savedD);

我希望这个配置,我在SerializationConfig上设置视图,应用于所有对象用这个ObjectMapper映射。

I would expect this configuration, were I set the view on the SerializationConfig, to be applied to all Objects that are mapped with this ObjectMapper.

如何使ObjectMapper始终应用JsonView而不必调用 writerWithView 这样我就可以把这个ObjectMapper交给Spring MVC了?

How can I make an ObjectMapper always apply a JsonView without having to call writerWithView so that I can just give this ObjectMapper to Spring MVC?

推荐答案

如果你真的读过你发现的那些文档,那么原来是这样的不能只是通过调用getSerializationConfig并在其上调用setter来改变序列化配置。

Turns out if you actually read the docs you find out that you can't just change serialization configuration by calling getSerializationConfig and calling setters on it.

/**
 * Method that returns the shared default {@link SerializationConfig}
 * object that defines configuration settings for serialization.
 *<p>
 * Note that since instances are immutable, you can NOT change settings
 * by accessing an instance and calling methods: this will simply create
 * new instance of config object.
 */
SerializationConfig getSerializationConfig();

我再说一遍, 你不能通过访问实例和调用方法来更改设置:

I repeat, you can NOT change settings by accessing an instance and calling methods:

所以你必须调用 .configure 方法而不是 .without 并且您无法通过调用 .withView 来设置视图。这些方法将构造一个SerializationConfig的新实例,但是没有办法让你的新SerializationConfig重新进入ObjectMapper。

So you must call the .configure method instead of .without and you cannot set the view by calling .withView. Those methods will construct a new instance of SerializationConfig, but there is no way to get your new SerializationConfig back into the ObjectMapper.

为了解决这个问题并连接我的ObjectMapper当它处理@ResponseBody时由Spring MVC使用我实现了以下内容:

In order to get around that and wire my ObjectMapper up to be used by Spring MVC when it processes a @ResponseBody I implemented the following:

  @Configuration
  class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
      ObjectMapper mapper = new ObjectMapper() {
        private static final long serialVersionUID = 1L;
        @Override
        protected DefaultSerializerProvider _serializerProvider(SerializationConfig config) {
          // replace the configuration with my modified configuration.
          // calling "withView" should keep previous config and just add my changes.
          return super._serializerProvider(config.withView(Views.Public.class));
        }        
      };
      mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
      converter.setObjectMapper(mapper);
      converters.add(converter);
    }
  }

有了这些,一切都对我有用。

With all that in place, everything is working for me.

这篇关于Jackson JsonView未被应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆