15

I am completely a beginner at Spring (you can see that in my code :) ). I just wanted to test the class RestTemplate but I got a ClassNotFoundException.
So the code is:

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();
    }
}

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>

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>

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

Exception

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

I have this exception when I try to run the main method from eclipse.
I can think of something like the spring jars cannot be seen but I don't know why... Can you please help me?

Dónal
  • 185,044
  • 174
  • 569
  • 824
Viktor
  • 1,325
  • 2
  • 19
  • 41
  • Do you have the Sprin-web jar in your classpath ? – SiN Jan 07 '14 at 11:11
  • Hi SiN, thanks for help! The spring-web is in the maven dependencies. I thought the maven dependencies are on the classpath in eclipse. or am I mistaken? – Viktor Jan 07 '14 at 11:48
  • spring 4.0.0 ? I can see that class should in spring-web.jar as of 3.0.x may be its not there in 4.0 ? – mzzzzb Jan 07 '14 at 11:48
  • I found the class MappingJacksonHttpMessageConverte‌ in eclipse in the maven dependencies (spring-web-4.0.0.jar). However I tried the spring 3.0.5 too but the result was the same... :( – Viktor Jan 07 '14 at 11:58

7 Answers7

29

The first major version of Jackson is no longer supported in Spring 4. The class you want to use is now org.springframework.http.converter.json.MappingJackson2HttpMessageConverter. Make sure that you have com.fasterxml.jackson.core/jackson-core/2.x.x on your classpath.

James McShane
  • 779
  • 8
  • 15
  • Thanks, this solved my problem. Just for the information we need to add 3 jars in classpath becasue of its dependency on each other: 1. jackson-core-x.x.x.jar, 2. jackson-databind-x.x.x.jar, 3. jackson-annotations-x.x.x.jar – Chintan Patel May 07 '15 at 07:19
10

I faced same issue. Fixed using org.springframework.http.converter.json.MappingJackson2HttpMessageConverter instead of org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌r

Jens
  • 67,715
  • 15
  • 98
  • 113
Susobhan Das
  • 125
  • 1
  • 3
8

i tried to copy paste your spring beans to a project but something strange is wrong with

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

this line, specifically there seems to be some invisible characters before the last r in Converter try typing that classname again manually.

if this is the case then its the craziest thing i have seen for sometime :D

Also MappingJacksonHttpMessageConverter is deprecated in 4.0.0 there is something newer. And you will need to add dependencies for jackson as well to get things working. This should be helpful.

Community
  • 1
  • 1
mzzzzb
  • 1,422
  • 19
  • 38
  • that would explain the title of this post (the gap before the r) too. the .class file is in spring-web. It may be damaged in the local repo? – wemu Jan 07 '14 at 13:37
  • ya i thought it was a typo there. i think OP copied from some websource which had those characters there. i copied this into eclipse and i wouldnt notice anything different. i kept staring at the classname for like whole 5 mins :D – mzzzzb Jan 07 '14 at 13:39
  • now i notice that in the stacktrace `r` is colored differently too – mzzzzb Jan 07 '14 at 13:43
  • Oh my God! ... I really don't know how I did this... :) You are right, there was ... something before the letter 'r' but I don't know what. Anyway sorry about the confusion and thank you for the help! Now it is working. – Viktor Jan 07 '14 at 13:53
  • it can be a nightmare debugging things like this. i was like whats the different between `MappingJacksonHttpMessageConverte‌​‌​r` and `MappingJacksonHttpMessageConverter` :D – mzzzzb Jan 07 '14 at 13:56
2

You need to add the following to your pom.xml (not parent pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>
Wins
  • 3,420
  • 4
  • 36
  • 70
  • This is a possible option when spring autoconfigure was in the project as well. But looks like it is not related to the original question. – amisiuryk Oct 08 '19 at 10:15
2

I had the issue, got it fixed by adding following dependencies in pom.xml

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>

Found it here!

Community
  • 1
  • 1
Pallav Jha
  • 3,409
  • 3
  • 29
  • 52
  • 1
    **Anyone is pulling hairs because of this** - key is that you should use jackson version 2.6.3+, especially when you are using Spring version 4.2.6+ .. I tried with many versions of jackson library and still had this issue, but as soon as I used version 2.6.3 this issue got resolved, so this means that earlier versions of jackson library references `org.springframework.http.converter.json.MappingJacksonHttpMessageConverter` but since in hire versions of Spring this is replaced by `MappingJackson2HttpMessageConverter`, so there will be `ClassNotFoundException:MappingJacksonHttpMessageConverter` – hagrawal7777 Jul 18 '16 at 14:09
  • i dont know what this answer fixes, but it is surely not this question. – Markus Kull Jan 02 '17 at 14:29
0

Programmaticaly you can do your configuration like this:

public class AppConfiguration {

   ...
    @Bean
    public HttpMessageConverters customConverters() {
        HttpMessageConverter<?> jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
//        HttpMessageConverter<?> another = ...
        return new HttpMessageConverters(jacksonMessageConverter);
    }
}
Picrochole
  • 167
  • 5
0
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

it's worked for me