Thursday, August 3, 2017

Resolving Maven Dependency Conflict Problem In Intellij

After deploying Spring Boot application, there's error in log complaining:
2017-04-06 10:46:14.890 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet dispatcherServlet threw exception

java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonGenerator.writeStartObject(Ljava/lang/Object;)V
        at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:515) ~[jackson-databind-2.8.5.jar!/:2.8.5]
This is 99% likely causing from maven dependency conflict in most scenarios.
The way to solve it is always checking dependency conflict before deploying. In Intellij Idea, there's a plugin callMaven Helper, after installing, open the pom.xml file and you could find adependency analyzertab downside.
As we can see, there's two version of jackson-core packages. After going to github to check the method there, we find that 2.8.5 has methodwriteStartObject(Object forValue)whereas 2.6.6 only haswriteStartObject(). From the error above, we must exclude 2.6.6 for sure, which could be done easily independency analyzer, just right-click on the version you intend to exclude and select.

After deploying in docker from Elastic Beanstalk again, the error is still there, whereas if I run the jar file locally, there's no error. This is more interesting. I print thejackson-coredependency that is being used at runtime via the following java command:
System.out.println("FLAG123="+com.fasterxml.jackson.core.JsonGenerator.class.getProtectionDomain().getCodeSource().getLocation());
In this way, I could safely conclude that no error should be complained in docker deployment provided that both runtimejackson-coreis the same. And actually, my local version uses one of the other module's jar file as dependency. After excluding it from my pom.xml, all works fine again.

1 comment: