2011-05-30 31 views
5

Lo que pretendo hacer es configurar spring mvc 3 para no devolver el objeto "nulo" en la respuesta json. He hecho la pregunta how to configure spring mvc 3 to not return "null" object in json response?. Y la sugerencia que recibí es configurar ObjectMapper, estableciendo la inclusión de serialización en JsonSerialize.Inclusion.NON_NULL. Por lo tanto, basado en Spring configure @ResponseBody JSON format, hice los siguientes cambios en el archivo de configuración de primavera. Pero recibí el error "Nombre del bean rechazado 'jacksonObjectMapper': no ​​hay rutas URL identificadas org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping: 86-AbstractDetectingUrlHandlerMapping.java" durante el inicio de la aplicación.configurando el jacksonObjectMapper no funciona en mvc de primavera 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> 

No tengo idea de por qué ha sido rechazada. ¡Cualquier sugerencia sera grandemente apreciada!

Respuesta

7

<mvc:annotation-driven /> y AnnotationMethodHandlerAdapter no se pueden utilizar juntas. (ref: spring forum thread). Posible solución

  1. no utilice <mvc:annotation-driven/>. Declaración de bean: DefaultAnnotationHandlerMapping y AnnotationMethodHandlerAdapter y otras configuraciones como validación, formateo.

  2. uso de primavera 3.1, que tiene <mvc:message-converters> (ref: Spring jira)

+0

encontré [esta entrada del blog] (http://software.sawano.se/2012/03/controlling-message-converters -with.html) útil al tratar de descubrir cómo usar el elemento ''. – DuffJ

Cuestiones relacionadas