简体   繁体   English

在 Vert.x 中使用 Jackson 的 JSON Java 8 LocalDateTime 格式

[英]JSON Java 8 LocalDateTime format using Jackson in Vert.x

I am trying to create json object from LocaLDateTime but for some reason it is creating json like so, look for issueAt and expireAt key我正在尝试从 LocalDateTime 创建 json 对象,但由于某种原因,它正在像这样创建 json,查找 issueAt 和 expireAt 键

json {"userID":0,"deviceID":0,"refreshToken":"93180548-23b3-4d1b-8b5b-a105b7cff7f9", "issuedAt" :{"year":2021,"monthValue":10,"dayOfMonth":27,"hour":9,"minute":22,"second":31,"nano":0,"month":"OCTOBER","dayOfWeek":"WEDNESDAY","dayOfYear":300,"chronology":{"id":"ISO","calendarType":"iso8601"}}, "expiresAt" :{"year":2021,"monthValue":10,"dayOfMonth":28,"hour":9,"minute":22,"second":31,"nano":0,"month":"OCTOBER","dayOfWeek":"THURSDAY","dayOfYear":301,"chronology":{"id":"ISO","calendarType":"iso8601"}}} json {"userID":0,"deviceID":0,"refreshToken":"93180548-23b3-4d1b-8b5b-a105b7cff7f9", "issuedAt" :{"year":2021,"monthValue":10,"dayOfMonth" :27,"hour":9,"minute":22,"second":31,"nano":0,"month":"OCTOBER","dayOfWeek":"WEDNESDAY","dayOfYear":300,"年表":{"id":"ISO","calendarType":"iso8601"}}, "expiresAt" :{"year":2021,"monthValue":10,"dayOfMonth":28,"hour":9 ,"minute":22,"second":31,"nano":0,"month":"OCTOBER","dayOfWeek":"THURSDAY","dayOfYear":301,"chronology":{"id": "ISO","calendarType":"iso8601"}}}

I want it to be like so我希望它像这样

batch: [0,0,29a1bf70-648e-4cb5-aef8-5377cf702875, 2021-10-26T12:36:10,2021-10-27T12:36:10 ] .批次:[0,0,29a1bf70-648e-4cb5-aef8-5377cf702875, 2021-10-26T12:36:10,2021-10-27T12:36:10 ]。

My code for creating the 2 dates is below我创建 2 个日期的代码如下

    String randomString = UUID.randomUUID().toString();
    Instant myInstant1 = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    LocalDateTime issuedAt = LocalDateTime.ofInstant(myInstant1, ZoneId.systemDefault());
    System.out.println("issued_at : " + issuedAt);
    LocalDateTime expiresAt = issuedAt.plusDays(1);
    System.out.println("expires_at: " + expiresAt.plusDays(1));

In the below code is where I get the error when I try to use mapto to add the json object to my class object.在下面的代码中,当我尝试使用 mapto 将 json 对象添加到我的类对象时出现错误。

JsonObject json = new JsonObject()
                    .put("userID", userID)
                    .put("deviceID", deviceID)
                    .put("refreshToken", randomString)
                    .put("issuedAt", issuedAt)
                    .put("expiresAt", expiresAt);
                                    
                                
LOG.info("json {}", json.encode());

RefreshToken refreshTokenObj = json.mapTo(RefreshToken.class); //here I am trying to mapTo my class and I get the error
LOG.info("refreshTokenObj {}", refreshTokenObj);

The error I get is我得到的错误是

2021-10-27 09:22:31.133+0330 [vert.x-eventloop-thread-1] ERROR com.galiy.main.MainVerticle - Unhandled: java.lang.IllegalArgumentException: Cannot construct instance of java.time.LocalDateTime (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: UNKNOWN; 2021-10-27 09:22:31.133+0330 [vert.x-eventloop-thread-1] 错误 com.galiy.main.MainVerticle - 未处理:java.lang.IllegalArgumentException:无法构造java.time.LocalDateTime实例(不存在创建者,如默认构造函数):无法从 [来源:未知; line: -1, column: -1] (through reference chain: com.galiy.security.refreshToken.RefreshToken["issuedAt"]) at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4236) ~[jackson-databind-2.11.4.jar:2.11.4]行:-1,列:-1](通过参考链:com.galiy.security.refreshToken.RefreshToken["issuedAt"])在 com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:4236) ~ [jackson-databind-2.11.4.jar:2.11.4]

and my RefreshToken model is like so,我的 RefreshToken 模型是这样的,

public class RefreshToken {

private Integer id;
private Integer userID;
private Integer deviceID;
private String refreshToken;
private LocalDateTime issuedAt;
private LocalDateTime expiresAt;

I am not familiar with Vert.x .我不熟悉Vert.x But according to our discussion under the post, I simply add following 2 line of code before mapTo() and got no error.但是根据我们在帖子下的讨论,我只是在mapTo()之前添加了以下 2 行代码并且没有错误。

ObjectMapper objectMapper = DatabindCodec.mapper();
objectMapper.registerModule(new JavaTimeModule());

Console output:控制台输出:

RefreshToken{id=null, userID=0, deviceID=0, refreshToken='9da220ce-bc66-4561-b924-988c7f394f2d', issuedAt=2021-10-27T17:21:28, expiresAt=2021-10-28T17:21:28} RefreshToken{id=null, userID=0, deviceID=0, refreshToken='9da220ce-bc66-4561-b924-988c7f394f2d',issuedAt=2021-10-27T17:21:28, expiresAt=2021-10-21:T17:2 28}


And in my experience, you can also configure ObjectMapper to handle the output format of LocalDateTime as you want while serialization as follows:根据我的经验,您还可以配置ObjectMapper以在序列化时根据需要处理LocalDateTime的输出格式,如下所示:

objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

You will need to annotate your LocalDateTime member as follows:您需要按如下方式注释 LocalDateTime 成员:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
LocalDateTime myTime

Here is the link to full answer that explains all the details: Spring Data JPA - ZonedDateTime format for json serialization这是解释所有细节的完整答案的链接: Spring Data JPA - ZonedDateTime format for json serialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM