Error "java.lang.NoSuchFieldError: WRITE_DURATIONS_AS_TIMESTAMPS"

I am working on a Java Web Service with Maven depending on another java project. I will name the web service project (project 2) and the dependent project (project 1)

So, I have a project "1st project" that uses "com.fasterxml.jackson.datatype.joda.JodaModule". Project 1 has these 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 to use "Project 1" in "project 2". Therefore i am writing 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 message from the client. it gives 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 am trying to use the same code in the Maven Java Project, and not in the Maven Web Java Project, it works in the 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); } } 

change 1:

I was working on glassfish 4.1, and when I change the server to tomcat, it works. therefore, I think there are banners of conflict in the glass planet. but so far I do not know how I can resolve this conflict.

+7
source share
2 answers

This is due to a mismatch of dependencies between Jackson Libs and Joda. It works for you when only the original Maven module is used, because it declares all Jackson libraries as 2.6.2, which is correct. Your bundled web application probably uses a different version of Jackson as a direct or transitive dependency. This leads to incompatible banks on your way to classes.

To check: just check your WEB-INF / lib and find all the libraries related to Jackson. You will not find version 2.6.2.

Bugfix: you need to decide if you can upgrade to the new version in your web application. Then configure the appropriate Maven dependencies.

+16
source

It looks like a conflict of dependence. Make sure that both projects download the same version of joda-time (and its dependencies).

You can view the dependency tree by running mvn dependency:tree on the command line or by looking at the Dependency Hierarchy panel when opening pom.xml with the POM editor in Eclipse.

+2
source

Source: https://habr.com/ru/post/1238490/


All Articles