gpt4 book ai didi

spring-mvc - 如何在spring mvc 3.2中启用@JsonRootName

转载 作者:行者123 更新时间:2023-12-04 04:58:40 24 4
gpt4 key购买 nike

我正在使用 Spring 3.2,我的 Spring MVC Controller 生成 JSON 数据(使用 jackson-databind-2.2.0)。我想使用 @JsonRootName (com.fasterxml.jackson.annotation.JsonRootName) 注释自定义我的 JSON 根名称,但是,我不知道如何使用 Spring 配置启用它。

@JsonRootName("rootNameTest")
public class MyModel {
private String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
}

这是我在 sevlet-context.xml 中的设置
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="contentNegotiationManager">
<bean class="org.springframework.web.accept.ContentNegotiationManager">
<constructor-arg>
<bean class="org.springframework.web.accept.ParameterContentNegotiationStrategy">
<constructor-arg>
<map>
<entry key="json" value="application/json"/>
</map>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
</list>
</property>
</bean>

请帮忙。谢谢。

最佳答案

Setbelow in com.fasterxml.jackson.databind.ObjectMapper

om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
om.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

这可以通过使用您的自定义扩展上面的类并注入(inject) org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 来完成

关于spring-mvc - 如何在spring mvc 3.2中启用@JsonRootName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16427375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com