java - Jackson 与 JodaTime 和 Jax-rs

标签 java rest jackson jax-rs

我有一个用 Java 创建的 REST Web 服务。我正在使用Joda-time日期和 Jackson用于 JSON 格式。一切都上传到 Glassfish 4.1 服务器上

版本

  • avax.ws.rs-api-2.0.1.jar
  • joda-time-2.7.jar
  • jackson-annotation-2.8.8.jar
  • jackson-core-2.8.8.jar
  • jackson-databind-2.8.8.jar
  • jackson.datatype-joda-2.8.8.jar

映射器

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper>{

    final ObjectMapper mapper = new ObjectMapper();

    public ObjectMapperContextResolver() {
        mapper.registerModule(new JodaModule());
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }  
}

调用服务时出错

java.lang.NoSuchFieldError: WRITE_DURATIONS_AS_TIMESTAMPS

我发现了什么

我已经found不同版本之间可能不匹配。所有的 jar 都来自 Maven 存储库,我每次都获取所需的依赖项。

问题

我错过了什么?是否缺少图书馆?库版本是否错误?

注意:我没有使用 Maven

更新

我尝试更新glassfish4.1/glassfish/modules内的jackson-?.jars,但现在由于要求,我什至无法启动服务器与 jackson 版本不匹配

更新 2

有没有一种方法可以使用我的项目中的 Jackson 库而不是 Glassfish 中的库?这似乎是解决方案

最佳答案

Is there a way to use the Jackson libraries that are inside my project instead of the one in Glassfish? This seems to be the solution.

请参阅 GlassFish 4 Application Development Guide 第 2 章中的以下引用:

The Java Servlet specification recommends that a web module's class loader look in the local class loader before delegating to its parent. You can make this class loader follow the delegation inversion model in the Servlet specification by setting delegate="false" in the class-loader element of the glassfish-web.xml file. It is safe to do this only for a web module that does not interact with any other modules. [...]

The default value is delegate="true", which causes a web module's class loader to delegate in the same manner as the other class loaders. You must use delegate="true" for a web application that accesses EJB components or that acts as a web service client or endpoint. [...]

For a number of packages, including java.* and javax.*, symbol resolution is always delegated to the parent class loader regardless of the delegate setting. This prevents applications from overriding core Java runtime classes or changing the API versions of specifications that are part of the Java EE platform.

GlassFish 4 Application Deployment Guide 的 B 部分中您将找到 glassfish-web.xml 部署描述符的示例。根据您的问题进行定制,您的 glassfish-web.xml 文件将如下所示:

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Servlet 3.0//EN"
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <class-loader delegate="false" />
</glassfish-web-app>

然后将其放在 Web 模块的 WEB-INF 下。

关于java - Jackson 与 JodaTime 和 Jax-rs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44628900/

相关文章:

java - 可重入读写锁同步上是 Write Locked

java - 在 Maven 构建期间提供三个属性文件作为输入

java - Spark read() 有效,但 sql() 抛出数据库未找到

php - 如何编写 REST API?

c# - 如何让 ModelState 在 asp.net mvc web api 上工作?

java - Jackson:通过 writeValueAsString 对象到字符串:NPE

java - 相当于 string.isempty 的 Int

linux - 是否有用于 Linux 手册页的(RESTful)API?

java - Jackson ObjectMapper 即使使用 NON_NULL 也会抛出 NullPointerException

java - JPA ManyToMany 关系和 JSON 序列化