配置jacksonObjectMapper在spring mvc 3中不起作用 [英] configuring the jacksonObjectMapper not working in spring mvc 3
问题描述
我的目标是将spring mvc 3配置为不在json响应中返回null对象。
我问了一个问题如何配置spring mvc 3不返回" null" json响应中的对象?。我得到的建议是配置ObjectMapper,将序列化包含设置为JsonSerialize.Inclusion.NON_NULL。所以基于 Spring配置@ResponseBody JSON格式,我在spring配置中做了以下更改文件。但我在应用启动期间收到错误拒绝的bean名称'jacksonObjectMapper':没有识别出URL路径org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:86-AbstractDetectingUrlHandlerMapping.java。
What I am aiming to do is to configure spring mvc 3 to not return "null" object in json response. I've asked the question how to configure spring mvc 3 to not return "null" object in json response? . And the suggestion I got is to configure the ObjectMapper, setting the serialization inclusion to JsonSerialize.Inclusion.NON_NULL. So Based on Spring configure @ResponseBody JSON format, I did the following changes in spring config file. But I got the error "Rejected bean name 'jacksonObjectMapper': no URL paths identified org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping:86-AbstractDetectingUrlHandlerMapping.java" during app startup.
<?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>
我不知道为什么它被拒绝了。非常感谢任何建议!
I have no idea why it's been rejected. Any suggestion is greatly appreciated!
推荐答案
< mvc:annotation-driven />
和 AnnotationMethodHandlerAdapter
不能一起使用。 (参考: spring forum线程)。可能的解决方案
<mvc:annotation-driven />
and AnnotationMethodHandlerAdapter
cannot be used together. (ref: spring forum thread). Possible solution
-
不要使用
< mvc:annotation-driven />
。声明bean:DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter
以及其他设置,如验证,格式化。
do not use
<mvc:annotation-driven/>
. Declaring bean:DefaultAnnotationHandlerMapping
andAnnotationMethodHandlerAdapter
and other settings like validation, formatting.
使用spring 3.1,它有< mvc:message-converters>
(参考: Spring jira )
use spring 3.1, which has <mvc:message-converters>
(ref: Spring jira)
这篇关于配置jacksonObjectMapper在spring mvc 3中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!