【问题标题】:configuring the jacksonObjectMapper not working in spring mvc 3配置jacksonObjectMapper在spring mvc 3中不起作用
【发布时间】:2011-09-04 21:29:24
【问题描述】:

我的目标是配置 spring mvc 3 在 json 响应中不返回“null”对象。 我已经问过how to configure spring mvc 3 to not return "null" object in json response? 的问题。我得到的建议是配置ObjectMapper,将序列化包含设置为JsonSerialize.Inclusion.NON_NULL。所以基于Spring configure @ResponseBody JSON format,我在spring config文件中做了如下改动。但是我在应用程序启动期间收到错误“拒绝的 bean name 'jacksonObjectMapper': no URL paths identify org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:86-AbstractDetectingUrlHandlerMapping.java”。

<?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>

我不知道为什么它被拒绝了。非常感谢任何建议!

【问题讨论】:

    标签: json spring-mvc jackson


    【解决方案1】:

    &lt;mvc:annotation-driven /&gt;AnnotationMethodHandlerAdapter 不能一起使用。 (参考:spring forum thread)。可能的解决方案

    1. 不要使用&lt;mvc:annotation-driven/&gt;。声明 bean:DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter 和其他设置,如验证、格式化。

    2. 使用spring 3.1,它有&lt;mvc:message-converters&gt;(参考:Spring jira

    【讨论】:

    • 我发现this blog post 在尝试弄清楚如何使用&lt;mvc:message-converters&gt; 元素时很有用。
    猜你喜欢
    • 2015-04-15
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多