10

I'm working on Java Web Service with Maven depending on another java project. I will call the Web Service Project (Project 2), and the depending project (Project 1)

So I have a "project 1" this project using "com.fasterxml.jackson.datatype.joda.JodaModule" jar. "Project 1" have this dependencies.

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0-rc2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>3.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.skyscreamer</groupId>
        <artifactId>jsonassert</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.yammer.dropwizard</groupId>
        <artifactId>dropwizard-testing</artifactId>
        <version>0.6.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Now I need using "Project 1" in "project 2". So I write this code

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new ISO8601DateFormat());
    mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    JodaModule module = new JodaModule();
    mapper.registerModule(module);

}

but when I call the Post from the client. it give me this error.

Warning:   StandardWrapperValve[Recieve]: Servlet.service() for servlet Recieve threw exception
java.lang.NoSuchFieldError: WRITE_DURATIONS_AS_TIMESTAMPS
    at com.fasterxml.jackson.datatype.joda.ser.DurationSerializer.<init>(DurationSerializer.java:28)
    at com.fasterxml.jackson.datatype.joda.ser.DurationSerializer.<init>(DurationSerializer.java:25)
    at com.fasterxml.jackson.datatype.joda.JodaModule.<init>(JodaModule.java:45)
    at Recieve.doPost(Recieve.java:82)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

Note: I try the same code in Maven Java Project not Maven Web Java Project, it is working in Maven java project.

public class test {

public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new ISO8601DateFormat());
    mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    JodaModule module = new JodaModule();
    mapper.registerModule(module);
    }
}

edit 1:

I was working on glassfish 4.1 and when I change the server to tomcat it works. so I think there is a conflict jars in glassfish. but till now I don't know how I can solve this conflict.

user1927468
  • 1,033
  • 2
  • 10
  • 14
  • I had similar issue when reordered dependencies in maven, so one lib has included jackson with higher version then mine, it became 1st and broke tests. Rolling back ordering or excluding dependencies. – Yaro Nov 23 '18 at 13:57

3 Answers3

20

This is caused by a dependency mismatch between jackson libs and joda. It is working for you when using only the original Maven module because it declares all the jackson libs as 2.6.2, which is correct. Your enclosing webapp surely uses some another jackson version either as direct or transitive dependency. That leads to incompatible jars being on your classpath.

To verify: just check your WEB-INF/lib and look for all libs that are jackson related. You will find non 2.6.2 versioned ones.

To fix: you need decide if you are able to upgrade to a new version on your webapp. Then configure your Maven dependencies accordingly.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • I think this is the problem. please check the edit in the question. – user1927468 Dec 17 '15 at 09:02
  • According to this answer: http://stackoverflow.com/a/7398607/821786 you can instruct the GlassFish class-loader to give precedence to your libs. – Gergely Bacso Dec 17 '15 at 19:36
  • I had this issue when using the maven plugin "war:inplace" after upgrading from jackson 2.3.* to 2.8.* both JARs were in the WEB-INF/lib directory. I had to manually remove the older dependency. – Benjamin Slabbert Jul 21 '17 at 08:51
2

It looks like a dependency conflict. Ensure that both projects load the same joda-time (and its dependencies) versions.

You can have a look at the dependency tree by running mvn dependency:tree in command line or by having a look at the Dependency Hierarchy panel when opening the pom.xml with the POM editor in Eclipse.

Marco Ferrari
  • 4,914
  • 5
  • 33
  • 53
  • It's worth looking for jersey dependencies on Jackson to get this fixed – MonoThreaded Jun 12 '16 at 20:45
  • Thanks. I found it using `mvn dependency:tree` . it is because of wrong version of com.fasterxml.jackson.datatype.jackson-datatype-joda jar being loaded. in my case the correct version is 2.7.2 – Mohamed Uvais M Feb 19 '19 at 08:41
0

You should check your glassfish lib folders and search for jackson.jar. Then delete those jar or use the version of Jackson version.

After deleting these jars its working fine.

enter image description here

Arvind Chavhan
  • 488
  • 1
  • 6
  • 14