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

2022-09-03 08:42:36

我完全是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();
    }
}

上下文.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>

啪.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>

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

例外

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

当我尝试从 eclipse 运行 main 方法时,我有这个例外。
我能想到像春天罐子看不见的东西,但我不知道为什么......你能帮帮我吗?


答案 1

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


答案 2

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


推荐