我想将 Jackson (2.7.4) 配置为在 Spring (4.2.6) MVC Controller 中缩进输出( pretty-print )。
我的 Controller 具有 @ResponseBody,当然它会转换为 JSON。我正在使用 context.xml 文件。到目前为止我有这个:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!--- WHAT GOES HERE -->
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
我想像这样设置 ObjectMapper 的configure(SerializationFeature f, boolean state):
configure(SerializationFeature.INDENT_OUTPUT, TRUE)
如何在 Spring 上下文中执行此操作?
最佳答案
您可以使用Jackson2ObjectMapperFactoryBean来配置ObjectMapper实例
示例
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:failOnEmptyBeans="false"
p:indentOutput="true">
<!-- Other properties -->
</bean>
</property>
关于java - 在 spring 配置中设置 Object Mapper SerializationFeature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37716353/