2013-05-07 85 views
2

我使用Spring 3.2和我的Spring MVC控制器生成JSON数据(与杰克逊数据绑定-2.2.0)。我想用@JsonRootName(com.fasterxml.jackson.annotation.JsonRootName)注释来定制我的JSON根名称,但是,我无法弄清楚如何使用Spring配置来启用它。如何启用@JsonRootName在春天mvc 3.2

@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> 

请帮助。谢谢。

回答

3

Setbelow在com.fasterxml.jackson.databind.ObjectMapper

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

这可能与您的自定义类以上的延伸来完成,并注入在org.springframework.http.converter.json.MappingJackson2HttpMessageConverter