使用MOXy解组LocalDate / LocalDateTime [英] Unmarshalling LocalDate/LocalDateTime with MOXy

查看:87
本文介绍了使用MOXy解组LocalDate / LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让MOXy将JSON解组为 LocalDate LocalDateTime

How can I get MOXy to unmarshal JSON into LocalDate and LocalDateTime?

我有一个 @GET 方法,该方法生成一个样本实例,其中包含三个类型 LocalDate LocalDateTime 日期

I've got an @GET method which produces a sample instance with three fields of types LocalDate, LocalDateTime and Date, respectively.

点击该终点,我得到:

{
    "localDate": "2017-07-11",
    "localDateTime": "2017-07-11T10:11:10.817",
    "date": "2017-07-11T10:11:10.817+02:00"
}

然后我将上述数据发布到我的 @POST 方法,它只是再次返回数据:

I then POST the above data to my @POST method, which simply returns the data again:

{
    "date": "2017-07-11T10:11:10.817+02:00"
}

正如你所看到的,两个 localDate localDateTime 在此过程中丢失,因为MOXy没有初始化这两个字段。

As you can see, both localDate and localDateTime are lost in the process, because MOXy does not initialize those two fields.

给出了什么? MOXy似乎支持这些类型的序列化,但不支持反序列化?

What gives? MOXy seems to support serialization of these types, but not deserialization?

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Path("/test/date")
public class DateTest {
    public static class Data {
        public LocalDate localDate;
        public LocalDateTime localDateTime;
        public Date date;
    }

    @GET
    @Path("roundtrip")
    public Response roundtrip() {
        Data sample = getSample();
        return roundtrip(sample);
    }

    @POST
    @Path("roundtrip")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response roundtrip(Data t) {
        return Response.status(Response.Status.OK).entity(t).build();
    }

    protected Data getSample() {
        final Data data = new Data();
        data.localDate = LocalDate.now();
        data.localDateTime = LocalDateTime.now();
        data.date = new Date();
        return data;
    }
}

Moxy版本: jersey- media-moxy-2.25.1

推荐答案

根据peeskillet的建议,我实现了以下适配器类:

According to peeskillet's suggestion I implemented the following adapter class:

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime>{

  private static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

  @Override
  public String marshal(LocalDateTime localDateTime) throws Exception {
    return localDateTime.format(DTF);
  }

  @Override
  public LocalDateTime unmarshal(String string) throws Exception {
    return LocalDateTime.parse(string, DTF);
  }

}

此外,我创建了 package-info.java 在同一个包中,我的MOXy类和适配器(在子包中)的位置包含以下内容:

In addition, I created package-info.java in the same package where my classes for MOXy and the adapter (in a subpackage) are located with the following content:

@XmlJavaTypeAdapters({
  @XmlJavaTypeAdapter(type=LocalDateTime.class,
      value=LocalDateTimeAdapter.class)
})
package api;

import java.time.LocalDateTime;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import api.adapter.LocalDateTimeAdapter;

因此,编组和解组工作没有问题。使用 DTF ,您可以指定应用的格式。

Thus, marshalling and unmarshalling works without problems. And with DTF you can specify the format that shall be applied.

这篇关于使用MOXy解组LocalDate / LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆