2010-09-01 58 views
1

標題幾乎說明了一切。現在它使用XML作爲來自我的控制器的響應中的默認值,但是當沒有指定Accepts頭或格式參數時,我希望它使用JSON作爲默認值。如何更改Spring MVC的默認媒體類型從XML到JSON

我的MVC-config.xml中

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="mediaTypes"> 

     <map> 
      <entry key="html" value="text/html" /> 
      <entry key="json" value="application/json" /> 
      <entry key="xml" value="application/xml" /> 
     </map> 
    </property> 
    <property name="defaultViews"> 
     <list> 
      <bean class="com.work.web.view.json.ExtendedMappingJacksonJsonView"> 
       <property name="objectMapper"> 
        <ref bean="JacksonObjectMapper" /> 
       </property> 
      </bean> 
      <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 
       <property name="marshaller"> 
        <ref bean="Jaxb2Marshaller" /> 
       </property> 
      </bean> 
     </list> 
    </property> 
    <property name="favorParameter" value="true" /> 
</bean> 

<bean 
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="messageConverters"> 
     <list> 
      <bean 
       class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
       <property name="objectMapper"> 
        <ref bean="JacksonObjectMapper" /> 
       </property> 
      </bean> 
      <ref bean="marshallingHttpMessageConverter" /> 
     </list> 
    </property> 
</bean> 

<bean id="marshallingHttpMessageConverter" 
    class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 
    <property name="marshaller" ref="Jaxb2Marshaller" /> 
    <property name="unmarshaller" ref="Jaxb2Marshaller" /> 
</bean> 
<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" /> 
<bean id="JacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" 
    factory-bean="JacksonObjectMapper" factory-method="getSerializationConfig" /> 
<bean 
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject" ref="JacksonSerializationConfig" /> 
    <property name="targetMethod" value="setSerializationInclusion" /> 
    <property name="arguments"> 
     <list> 
      <value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value> 
     </list> 
    </property> 
</bean> 
<bean id="Jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>com.work.Concepts</value> 

     </list> 
    </property> 
</bean> 

回答

4
<property name="defaultContentType" value="application/json" /> 

設立ContentNegotiatingViewResolver豆

相關問題