[英]java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava
I use very simple code that returns XML 我使用非常简单的代码返回XML
RestTemplate restTemplate = new RestTemplate();
Source oResponse = restTemplate.postForObject(url, entity, Source.class,
vars);
XPathOperations xpathTemplate = new Jaxp13XPathTemplate();
String address = xpathTemplate.evaluateAsString("//status", oResponse);
However, I get the following error 但是,出现以下错误
java.lang.NoSuchMethodError: org.springframework.util.ClassUtils.forName(Ljava/lang/String;)Ljava/lang/Class;
at org.springframework.xml.JaxpVersion.<clinit>(JaxpVersion.java:51)
at org.springframework.xml.transform.TraxUtils.isStaxSource(TraxUtils.java:70)
at org.springframework.xml.xpath.Jaxp13XPathTemplate.evaluate(Jaxp13XPathTemplate.java:131)
at org.springframework.xml.xpath.Jaxp13XPathTemplate.evaluateAsString(Jaxp13XPathTemplate.java:91)
Please help. 请帮忙。 Thanks, Vladimir 谢谢,弗拉基米尔
A NoSuchMethodError at runtime indicates that you are using a different version of a library than the version the code was built against. 运行时出现NoSuchMethodError表示您使用的库版本与生成代码所针对的版本不同。
In your case, Spring is the culprit. 在您的情况下,Spring是元凶。 Check what is on the classpath at runtime and ensure the following: 在运行时检查类路径上的内容,并确保以下各项:
Looking at http://docs.spring.io/spring/docs/3.2.0.M1/api/org/springframework/util/ClassUtils.html , it would appear that ClassUtils.forName(String) is deprecated as of Spring 3. My guess would be you have built your code against a version which had this method but are running it with a version where the method has been removed. 查看http://docs.spring.io/spring/docs/3.2.0.M1/api/org/springframework/util/ClassUtils.html ,看来从Spring 3开始不推荐使用ClassUtils.forName(String)。我的猜测是,您已经针对具有此方法的版本构建了代码,但是正在使用已删除该方法的版本运行代码。
The ClassUtils class is contained within the spring-core jar so I would ensure the same version of this jar is used at both compile and run time. ClassUtils类包含在spring-core jar中,因此我确保在编译和运行时都使用相同版本的jar。
We had a similar issue when we are migrating to Spring 4.1.1 迁移到Spring 4.1.1时,我们遇到了类似的问题
In our case, we had a dependency as follows 在我们的案例中,我们具有以下依赖性
<dependency>
<groupId>org.springframework.javaconfig</groupId>
<artifactId>spring-javaconfig</artifactId>
<version>1.0.0.m3</version>
</dependency>
We added this dependency inorder to support Post Processing of a bean 我们添加了此依赖关系以支持Bean的后期处理
<bean class="org.springframework.config.java.process.ConfigurationPostProcessor" />
There is a class from the above dependency "ProcessUtils.java", Which is invoking ClassUtils.forName with a Single argument (String). 上面的依赖项“ ProcessUtils.java”中有一个类,该类正在调用具有单个参数(字符串)的ClassUtils.forName。 However this method has been deprecated. 但是,不建议使用此方法。 So the Exeception is being thrown. 因此,正被抛出。
I have learnt from another post in stackoverflow that we do not need a ConfigurationPostProcessor with Spring 4.1.1 (am not sure of exact version, when this support is enabled) 我从stackoverflow的另一篇文章中了解到,在Spring 4.1.1中我们不需要ConfigurationPostProcessor(启用此支持后,我不确定确切的版本)
Once we removed the following from our Configuration (Spring Context file), the problem is resolved. 一旦从我们的配置(Spring Context文件)中删除了以下内容,问题就解决了。
You may have already figured this out but still in my case i was using a spring-orm whose version was different from spring-core and thus was getting similar error message when running unit tests. 您可能已经知道了这一点,但是在我的情况下,我仍使用的是Spring-orm,其版本不同于spring-core,因此在运行单元测试时收到类似的错误消息。 I have learnt from my experience that all the spring jar files included in a project should have the same version to avoid unnecessary issues. 我从经验中学到,项目中包含的所有spring jar文件都应具有相同的版本,以避免不必要的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.