2011-05-30 4 views
5

Ce que je vise à faire est de configurer spring mvc 3 pour ne pas retourner l'objet "nul" dans la réponse de JSON. J'ai posé la question how to configure spring mvc 3 to not return "null" object in json response?. Et la suggestion que j'ai eu est de configurer l'ObjectMapper, en définissant l'inclusion de sérialisation à JsonSerialize.Inclusion.NON_NULL. Donc, basé sur Spring configure @ResponseBody JSON format, j'ai fait les changements suivants dans le fichier de configuration de printemps. Mais j'ai reçu l'erreur "Nom de bean rejeté 'jacksonObjectMapper': aucun chemin d'URL identifié org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping: 86-AbstractDetectingUrlHandlerMapping.java" pendant le démarrage de l'application.configurer le jacksonObjectMapper ne fonctionne pas au printemps mvc 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> 

Je ne sais pas pourquoi cela a été rejeté. Toute suggestion est grandement appréciée!

Répondre

7

<mvc:annotation-driven /> et AnnotationMethodHandlerAdapter ne peuvent pas être utilisés ensemble. (ref: spring forum thread). Solution possible

  1. ne pas utiliser <mvc:annotation-driven/>. Déclarant le haricot: DefaultAnnotationHandlerMapping et AnnotationMethodHandlerAdapter et d'autres paramètres comme la validation, le formatage.

  2. utilisation du printemps 3.1, qui a <mvc:message-converters> (ref: Spring jira)

+0

J'ai trouvé ([ce blog] http://software.sawano.se/2012/03/controlling-message-converters -with.html) utile lorsque vous essayez de comprendre comment utiliser l'élément ''. – DuffJ

Questions connexes