Robert Bowen
Robert Bowen

Reputation: 487

How to set visibility for default ObjectMapper used by MappingJackson2JsonView, using Spring ContentNegotiatingViewResolver

I'd like to do what's suggested here, namely:

objectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);

Unfortunately the Json mapping for my app is done entirely in xml, like this:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
  <property name="defaultViews">
    <list>
      <bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
    </list>
  </property>
</bean>

I don't need a custom ObjectMapper, I'd just like to be able to set the visibility of the default ObjectMapper that is used by MappingJackson2JsonView.

Is there any way to do this?

Upvotes: 1

Views: 1066

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

You can't change the default ObjectMapper used by MappingJackson2JsonView. It is stored in a private field and no methods exist to modify the object.

However, you can declare your own ObjectMapper bean and use MappingJackson2JsonView#setObjectMapper(ObjectMapper) to have the View use your custom ObjectMapper bean.

Upvotes: 1

Related Questions