我想在我的Spring(4.2.6)MVC控制器中配置我的Jackson(2.7.4)缩进输出(漂亮打印)。
我的控制器有@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)
我如何在春天的环境中做到这一点?
答案 0 :(得分:2)
您可以使用Jackson2ObjectMapperFactoryBean配置ObjectMapper实例
实施例
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:failOnEmptyBeans="false"
p:indentOutput="true">
<!-- Other properties -->
</bean>
</property>
答案 1 :(得分:0)
你看起来像这样吗?
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.kulhade.config.CustomObjectMapper">
<constructor-arg type="com.fasterxml.jackson.databind.SerializationFeature" value="INDENT_OUTPUT"/>
<constructor-arg type="boolean" value="true"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
下面是CustomObjectMapper
public class CustomObjectMapper extends ObjectMapper{
public CustomObjectMapper(SerializationFeature feature,boolean value) {
this.configure(feature, value);
}
}