Java ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20969722/
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
ClassNotFoundException: org.springframework.http.converter.json.MappingHymansonHttpMessageConverter
提问by Viktor
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:
我完全是 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();
}
}
context.xml
上下文.xml
<beans ...>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????r"/>
</list>
</property>
</bean>
<bean id="restClient" class="org.googlecode.happymarvin.jiraexplorer.RestClient">
<property name="restTemplate" ref="restTemplate"/>
</bean>
</beans>
pom.xml
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>
<Hymanson-version>1.9.13</Hymanson-version>
</properties>
</project>
parent pom.xml
父 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.MappingHymansonHttpMessageConverte????r#77624896' of type [org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????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.MappingHymansonHttpMessageConverte????r] for bean with name 'org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????r#77624896' defined in class path resource [rest-client-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????r
...
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????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?
当我尝试从 eclipse 运行 main 方法时,出现此异常。
我可以想到无法看到弹簧罐之类的东西,但我不知道为什么......你能帮我吗?
采纳答案by mzzzzb
i tried to copy paste your spring beans to a project but something strange is wrong with
我试图将你的 spring beans 复制粘贴到一个项目中,但出现了一些奇怪的问题
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????r"/>
<bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverte????r"/>
this line, specifically there seems to be some invisible characters before the last r
in Converter
try typing that classname again manually.
这一行,特别是r
在Converter
尝试再次手动输入该类名时,在最后一行之前似乎有一些不可见的字符。
if this is the case then its the craziest thing i have seen for sometime :D
如果是这种情况,那么它是我一段时间以来见过的最疯狂的事情:D
Also MappingHymansonHttpMessageConverter
is deprecated in 4.0.0 there is something newer. And you will need to add dependencies for Hymanson as well to get things working. Thisshould be helpful.
MappingHymansonHttpMessageConverter
在 4.0.0 中也已弃用,还有一些更新。并且您还需要为 Hymanson 添加依赖项以使其正常工作。这应该会有所帮助。
回答by Wins
You need to add the following to your pom.xml (not parent pom.xml)
您需要将以下内容添加到您的 pom.xml(不是父 pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
回答by James McShane
The first major version of Hymanson is no longer supported in Spring 4. The class you want to use is now
org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter
. Make sure that you have com.fasterxml.Hymanson.core/Hymanson-core/2.x.x on your classpath.
Spring 4 不再支持 Hymanson 的第一个主要版本。您要使用的类现在是
org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter
. 确保您的类路径上有 com.fasterxml.Hymanson.core/Hymanson-core/2.xx。
回答by Picrochole
Programmaticaly you can do your configuration like this:
以编程方式,您可以像这样进行配置:
public class AppConfiguration {
...
@Bean
public HttpMessageConverters customConverters() {
HttpMessageConverter<?> HymansonMessageConverter = new MappingHymanson2HttpMessageConverter();
// HttpMessageConverter<?> another = ...
return new HttpMessageConverters(HymansonMessageConverter);
}
}
回答by Susobhan Das
I faced same issue. Fixed using org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter
instead of org.springframework.http.converter.json.MappingHymansonHttpMessageConverte?r
我遇到了同样的问题。固定使用org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter
代替org.springframework.http.converter.json.MappingHymansonHttpMessageConverte?r
回答by Obi Wan - PallavJha
I had the issue, got it fixed by adding following dependencies in pom.xml
我遇到了这个问题,通过在 pom.xml 中添加以下依赖项来解决它
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Found it here!
回答by zhenwodefengcai
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.6.3</version>
</dependency>
it's worked for me
它对我有用