ClassNotFoundException:org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

Vik*_*tor 12 java spring maven resttemplate

我完全是Spring的初学者(你可以在我的代码中看到:)).我只想测试RestTemplate类,但我得到了一个ClassNotFoundException.
所以代码是:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;
public class RestClient {
    private RestTemplate restTemplate;
    public String getJiraIssueAsJson(){
        Object o = restTemplate.getForObject(..., Object.class);
        System.out.println("..."+o.getClass());
        return null;
    }
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("rest-client-context.xml");
        RestClient restClient = context.getBean("restClient", RestClient.class);
        restClient.getJiraIssueAsJson();
    }
}
Run Code Online (Sandbox Code Playgroud)

的context.xml

<beans ...>
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r"/>
            </list>
        </property>
    </bean>
    <bean id="restClient" class="org.googlecode.happymarvin.jiraexplorer.RestClient">
        <property name="restTemplate" ref="restTemplate"/>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

的pom.xml

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.googlecode.happymarvin</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>jiraminer</artifactId>
    <name>Happy Marvin JIRA Miner</name>
    <packaging>jar</packaging>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jackson-version>1.9.13</jackson-version>
    </properties>
</project>
Run Code Online (Sandbox Code Playgroud)

父pom.xml

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.googlecode.happymarvin</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Happy Marvin parent project</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

例外

Jan 07, 2014 10:18:24 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@730eb2f0: startup date [Tue Jan 07 10:18:24 GMT 2014]; root of context hierarchy
Jan 07, 2014 10:18:24 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [rest-client-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [rest-client-context.xml]: Cannot create inner bean 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r#77624896' of type [org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r] for bean with name 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r#77624896' defined in class path resource [rest-client-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r
...
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r
Run Code Online (Sandbox Code Playgroud)

当我尝试从eclipse运行main方法时,我有这个异常.
我可以想到一些像春天的罐子看不到但我不知道为什么......你能帮助我吗?

Jam*_*ane 28

Spring 4不再支持Jackson的第一个主要版本.您现在要使用的是类 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.确保在类路径上有com.fasterxml.jackson.core/jackson-core/2.xx.


小智 9

我遇到了同样的问题.修复使用org.springframework.http.converter.json.MappingJackson2HttpMessageConverter而不是org.springframework.http.converter.json.MappingJacksonHttpMessageConverte?r


mzz*_*zzb 7

我试图复制粘贴你的春豆到项目,但奇怪的是错误的

<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte????r"/>

这条线,特别是似乎有之前的最后一些不可见的字符rConverter试着输入手动再次类名.

如果是这种情况,那么它是我见过的最疯狂的事情:D

同样MappingJacksonHttpMessageConverter在4.0.0中已弃用,有更新的东西.而且您还需要为jackson添加依赖项以使其工作正常.应该会有所帮助.