问题
As I have looked for an answer to this exception, but didn't find it anywhere, I'll leave this post here for future reference. So if anyone else runs into this problem, you are welcome.
I'm using the maven shade plugin to create a runnable jar together with org.springframework.data:spring-data-mongodb:1.4.0.RELEASE
I was running into the exception in the title, after creating the jar and running it with java -jar Foo-0.0.1-SNAPSHOT.jar
回答1:
And the answer:
The error is here: https://github.com/spring-projects/spring-data-mongodb/blob/master/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java in this line:
private static final boolean IS_SPRING_4_OR_BETTER = SpringVersion.getVersion().startsWith("4");
SpringVersion.getVersion returns null if it cannot retrieve the version number from the package. As the dependencies are extracted in the shaded jar, there is no package to retrieve the version number from and .startsWith("4") throws a NullPointerException.
To solve this issue (well it's kind of a quick and dirty solution, but it works), create a package org.springframework.core in your source folder and create the following class (I am using the springframework in version 4.0.2-RELEASE):
package org.springframework.core;
/**
* for spring data mongodb
* it can't determine the springversion in the shaded jar
*/
public class SpringVersion {
public static String getVersion() {
return "4.0.2-RELEASE";
}
}
The class will overwrite the original SpringVersion class file.
回答2:
Like @mininme mentioned in his answer, the problem arises in 4.0.2.RELEASE because of
SpringVersion.getVersion().startsWith("4").
For now it means, that if you have such a problem, you should probably try a more recent version of Spring. At version 4.3.0.RELEASE of Spring there in no such problem anymore.
来源:https://stackoverflow.com/questions/22201136/nullpointerexception-at-org-springframework-data-mongodb-core-convert-defaultdbr