错误" java.lang.NoSuchFieldError:WRITE_DURATIONS_AS_TIMESTAMPS"

时间:2015-12-17 08:00:29

标签: java maven

我正在使用Maven从事Java Web Service,具体取决于另一个java项目。我将调用Web服务项目(项目2)和依赖项目(项目1)

所以我有一个"项目1"这个项目使用" com.fasterxml.jackson.datatype.joda.JodaModule"罐。 "项目1"有这种依赖。

<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>

现在我需要使用&#34;项目1&#34;在&#34;项目2&#34;。所以我写了这段代码

@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);

}

但是当我从客户端拨打邮件时。它给了我这个错误。

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)

注意:我在Maven Java Project而不是Maven Web Java Project中尝试相同的代码,它在Maven java项目中工作。

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);
    }
}

编辑1:

我正在研究glassfish 4.1,当我将服务器更改为tomcat时,它可以工作。所以我认为glassfish中存在冲突罐子。但直到现在我还不知道如何解决这场冲突。

3 个答案:

答案 0 :(得分:17)

这是由于jackson libs和joda之间的依赖性不匹配造成的。当仅使用原始Maven模块时,它正在为你工作,因为它将所有jackson库声明为2.6.2,这是正确的。你的封闭webapp肯定使用另一个杰克逊版本作为直接或传递依赖。这会导致类似路径上存在不兼容的jar。

验证:只需检查您的WEB-INF / lib并查找与jackson相关的所有库。你会发现非2.6.2版本的。

要修复:您需要决定是否可以在网络应用上升级到新版本。然后相应地配置Maven依赖项。

答案 1 :(得分:2)

它看起来像是依赖冲突。确保两个项目都加载相同的joda-time(及其依赖项)版本。

您可以通过在命令行中运行mvn dependency:tree或在使用POM编辑器打开pom.xml时查看依赖关系层次结构面板来查看依赖关系树蚀。

答案 2 :(得分:0)

您应该检查您的glassfish lib文件夹并搜索jackson.jar。 然后删除这些jar或使用Jackson版本。

删除这些罐子后,其工作正常。

enter image description here