2013-05-07 33 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