格式化JAX-RS的LocalDateTime

时间:2018-05-17 14:12:37

标签: jax-rs resteasy localdate

我将resteasy-jaxrs与jackson-datatype-jsr310结合使用,以在响应中序列化LocalDateTime。这适用于我的类中的属性,因为我可以添加必要的注释,如

@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getExpirationDate() {
    return expirationDate;
}

使用LocalDateTimeAdapter

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
    @Override
    public LocalDateTime unmarshal(String s) throws Exception {
        return LocalDateTime.parse(s);
    }

    @Override
    public String marshal(LocalDateTime dateTime) throws Exception {
        return dateTime.toString();
    }
}

在json中,我得到类似

的内容
"expirationDate": "2026-07-17T23:59:59"

但是我有一个对象地图,这个地图也可以有LocalDateTime类型的项目,对于那些没有注释的项目,所以我在json响应中得到了完整的对象,如

"effectiveDate":       {
  "dayOfMonth": 1,
  "dayOfWeek": "FRIDAY",
  "month": "JUNE",
  "year": 2018, ...

有没有办法格式化每个LocalDateTime字段,无论它来自何处?

保罗回答后更新

由于我已经掌握了com.fasterxml.jackson.datatype:jackson-datatype-jsr310依赖项,我添加了ObjectMapperContextResolver类并从我的LocalDateTime属性中删除了注释。遗憾的是,所有LocalDateTimes现在再次被序列化为完整对象。在帖子的评论中,我看到有人在web.xml中添加了一个context-param,以便拾取ObjectMapperContextResolver。将此添加到我的web.xml后,它看起来像这样。

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>Servlet 3.1 Web Application</display-name>
    <listener>
        <listener-class>com.kopi.web.InitListener</listener-class>
    </listener>
    <context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>com.kopi.utils.ObjectMapperContextResolver</param-value>
    </context-param>
</web-app>

现在,自从添加了context-param resteasy.resources后,由于

,webapp不再启动了
SEVERE: Servlet [com.kopi.rest.RestApplication] in web application [/kopi] threw load() exception
java.lang.RuntimeException: RESTEASY003130: Class is not a root resource.  It, or one of its interfaces must be annotated with @Path: com.kopi.utils.ObjectMapperContextResolver implements:  javax.ws.rs.ext.ContextResolver
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:179)
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:158)
    at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:77)
    at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:482)
    at org.jboss.resteasy.spi.ResteasyDeployment.startInternal(ResteasyDeployment.java:279)
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:86)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:119)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1194)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1110)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1000)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4902)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5212)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:596)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1805)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

在其他评论中,我看到版本可能是一个问题。所以我目前正在使用

org.jboss.resteasy:resteasy-jaxrs 3.5.1.Final
org.jboss.resteasy:resteasy-jackson-provider 3.5.1.Final
org.jboss.resteasy:resteasy-servlet-initializer 3.5.1.Final
com.fasterxml.jackson.datatype:jackson-datatype-jsr310 2.9.5

谢谢你,kopi

2 个答案:

答案 0 :(得分:0)

您可以使用XmlAdapter在Jackson全局配置它,而不是您需要在各个属性上声明的ContextResolver。您可以在此处配置ObjectMapper并使用映射器注册JavaTimeModule。此配置将是全局的,因此您无需使用XmlAdapter。要了解如何配置此功能,您可以看到this post

答案 1 :(得分:0)

@XmlJavaTypeAdapter可以应用于整个包(see doc)。

  

在包级别定义@XmlJavaTypeAdapter注释时   适用于包中的所有引用   @ XmlJavaTypeAdapter.type()。

尝试在包含地图的类旁边添加文件package-info.java。鉴于类的包是com.acme.beans而适配器的包是com.acme.adapters,文件的内容应该是这样的:

@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
package com.acme.beans;

import com.acme.adapters.LocalDateTimeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;