2011-05-30 16 views
5

में काम नहीं कर रहे जैक्सनऑब्जेक्टमैपर को कॉन्फ़िगर करना जो मैं करना चाहता हूं वह है कि जेसन प्रतिक्रिया में "शून्य" ऑब्जेक्ट को वापस न करने के लिए वसंत एमवीसी 3 को कॉन्फ़िगर करना है। मैंने प्रश्न how to configure spring mvc 3 to not return "null" object in json response? से पूछा है। और मुझे जो सुझाव मिला है वह ऑब्जेक्टमैपर को कॉन्फ़िगर करना है, जेसनसेरियलइज़ को शामिल करना क्रमबद्ध करना .Inclusion.NON_NULL। तो Spring configure @ResponseBody JSON format के आधार पर, मैंने वसंत कॉन्फ़िगरेशन फ़ाइल में निम्न परिवर्तन किए। लेकिन मुझे त्रुटि मिली "अस्वीकृत बीन नाम 'जैक्सनऑब्जेक्टमैपर': कोई URL पथ पहचान नहीं है org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping: 86-AbstractDetectingUrlHandlerMapping.java" ऐप स्टार्टअप के दौरान।वसंत एमवीसी 3

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven /> 

    <!-- Forwards requests to the "/" resource to the "welcome" view --> 
    <!--<mvc:view-controller path="/" view-name="welcome"/>--> 

    <!-- Configures Handler Interceptors -->  
    <mvc:interceptors> 
     <!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de --> 
     <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> 
    </mvc:interceptors> 

    <!-- Saves a locale change using a cookie --> 
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> 


    <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="jacksonObjectMapper" /> 
       </bean> 
      </list> 
     </property> 
    </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_DEFAULT</value> 
      </list> 
     </property> 
    </bean> 


    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 

मुझे नहीं पता कि इसे क्यों अस्वीकार कर दिया गया है है। किसी भी सुझाव की सराहना की है!

उत्तर

7

<mvc:annotation-driven /> और AnnotationMethodHandlerAdapter एक साथ उपयोग नहीं किया जा सकता है। (रेफरी: spring forum thread)। संभावित समाधान

  1. <mvc:annotation-driven/> का उपयोग न करें। बीन घोषित करना: DefaultAnnotationHandlerMapping और AnnotationMethodHandlerAdapter और अन्य सेटिंग्स जैसे सत्यापन, स्वरूपण।

  2. उपयोग वसंत 3.1 है, जो है <mvc:message-converters> (संदर्भ: Spring jira)

+0

मैंने पाया [इस ब्लॉग पोस्ट] (http://software.sawano.se/2012/03/controlling-message-converters -with.html) '' तत्व का उपयोग करने का तरीका जानने का प्रयास करते समय उपयोगी। – DuffJ

संबंधित मुद्दे