java 使用 Spring4 找不到 MappingJacksonHttpMessageConverter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32356935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:05:04  来源:igfitidea点击:

MappingHymansonHttpMessageConverter not found with Spring4

javaspringspring-4

提问by Andrea Girardi

I migrated my Spring framework from 3.x to 4.2.RELEASE but, when I start the jUnit I'm getting this error:

我将 Spring 框架从 3.x 迁移到 4.2.RELEASE 但是,当我启动 jUnit 时,出现此错误:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:src/test/resources/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingHymansonHttpMessageConverter  

I read on internet from Version 4.1. it's not supported anymore; I checked and the new version is available on application class path (I imported spring-web dependency). ( Spring 4 and Rest WS integration)

我从 4.1 版开始在互联网上阅读。它不再受支持;我检查了新版本在应用程序类路径上可用(我导入了 spring-web 依赖项)。(Spring 4 和 Rest WS 集成

It seems spring is still looking for the old version of Converter. It probably depends on something I've on my application context, but, the question is, how can I "tell to Spring" to use new version?

看来spring还在寻找旧版本的Converter。这可能取决于我在应用程序上下文中的某些内容,但是,问题是,我如何“告诉 Spring”使用新版本?

-- UPDATE
I commented

- 更新
我评论

<mvc:annotation-driven />

and it seems to works fine but... why?

它似乎工作正常但是......为什么?

回答by Cornel Masson

There's a newer version of that class in Spring 4: use org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter(note the '2').

Spring 4 中有该类的更新版本:use org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter(注意“2”)。

In my REST servlet's Spring config file I have it configured as follows (I added my custom objectMapper to it, but you can just omit that):

在我的 REST servlet 的 Spring 配置文件中,我对其进行了如下配置(我向其中添加了我的自定义 objectMapper,但您可以省略它):

<mvc:annotation-driven>
    <mvc:message-converters>

        <bean 
            class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="s3.util.json.CustomJsonMapper" />
            </property>
        </bean>

    </mvc:message-converters>
</mvc:annotation-driven>