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

r9f1avp5  于 7个月前  发布在  Java
关注(0)|答案(3)|浏览(101)

我试图从LocaLDateTime创建json对象,但由于某种原因,它正在创建json,如下所示,查找issueAt和issureAt键
json {"userID ":0," deviceID ":0," refreshToken ":"93180548 - 23b3 - 4d1b-8b5b-a105b7cff7f9","issuedAt":{"year ":2021年," monthValue ":10," dayOfMonth ":27、“小时”:9、“分”:22、“第二”:31、"纳米":0,"月":" OCTOBER","dayOfWeek":"WEDNESDAY "," dayOfYear ":300,"年表":{" id":" ISO "、" BullarType ":"iso8601 "}},"默认值":{"year":2021年,"monthValue":10," dayOfMonth":28、“小时”:9、“分”:22、“第二”:31、"纳米":0,"月":" OCTOBER"," dayOfWeek":" THURSDAY"," dayOfYear":301,"年表":{" id":" ISO"、" BullarType":" iso8601"
我希望它是这样的
批次:[0,0,29a1bf70 - 648e-4cb5-aef8 - 5377cf702875,2021 - 10 - 26T12:36:10,2021 - 10 - 27T12:36:10]。
我的代码创建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));

在下面的代码中,当我试图使用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);

我得到的错误是
2021 - 10 - 27 09:22:31.133 + 0330 [vert. x-eventloop-thread-1]错误com. galiy. main. MainVerticle-未处理:java. lang. IllegalArgumentException:无法构造java.time.LocalDateTime的示例(不存在创建者,如默认构造函数):无法从[Source:未知;行:-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]
我的RefreshToken模型是这样的

public class RefreshToken {

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

wlp8pajw1#

我对Vert.x不熟悉。但根据我们在帖子下的讨论,我只是在mapTo()之前添加了以下两行代码,没有错误。

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

控制台输出:
RefreshToken{id=null,userID= 0,deviceID=0,refreshToken='9da220ce-bc66-4561-b924- 988c7f394f2d',issuedAt=2021-10-27T17:21:28,RefreshToken =2021-10-28T17:21:28}
根据我的经验,你也可以配置ObjectMapper来处理LocalDateTime的输出格式,同时序列化如下:

objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
piztneat

piztneat2#

您需要按如下方式注解LocalDateTime成员:

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

以下是完整答案的链接,解释了所有细节:Spring Data JPA - ZonedDateTime format for json serialization

relj7zay

relj7zay3#

默认情况下不支持java.time.LocalDateTime,因此我们必须在使用mapTo()函数之前注册模块。在json.mapTo(RefreshToken.class);之前添加此行-

DatabindCodec.mapper().registerModule(new JavaTimeModule());

相关问题