Jackson无法从Golang Api反序列化日期集合。

huangapple go评论74阅读模式
英文:

Jackson can't deserialize date set from Golang Api

问题

我正在开发一个使用基于Java的Cucumber测试进行BDD的Golang微服务。

在模式中有一个日期变量,定义如下:

startDate *time.Time

我将其设置为:

t := time.Now()
startDate = &t

当我通过Java程序运行BDD时,出现以下错误:

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2021-06-11T10:53:57.1124553+05:30": not a valid representation (error: Failed to parse Date value '2021-06-11T10:53:57.1124553+05:30': Unparseable date: "2021-06-11T10:53:57.1124553+05:30")

而在我的BDD测试中,我有:

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);

我对Golang相对较新,并且在处理日期时遇到了问题。

到目前为止,我尝试过以下方法:

t := time.Now().Format(time.RFC3339)
tt, _ := time.Parse(time.RFC3339, t)
startDate = &tt

我甚至尝试使用time.LoadLocation(),但是我无法找出问题所在。

英文:

I'm working on a Golang Micro-service which uses Java based Cucumber tests for BDDs.

There is a date variable inside the schema and it is defined as:

startDate *time.Time

I set this value as:

t := time.Now()
startDate = &t

When I run the BDDs through Java program, I get this error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2021-06-11T10:53:57.1124553+05:30": not a valid representation (error: Failed to parse Date value '2021-06-11T10:53:57.1124553+05:30': Unparseable date: "2021-06-11T10:53:57.1124553+05:30")

While in my BDD tests, I have:

private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(dateFormat);

I'm relatively new to Golang and facing issues while dealing with dates.

Until Now, I have tried:

t := time.Now().Format(time.RFC3339)
tt, _ := time.Parse(time.RFC3339, t)
startDate = &tt

I even tried with the time.LoadLocation(), but I am unable to figure out the issue.

答案1

得分: 1

你提供的Go代码不会影响Time实例的序列化方式,因为你在将其序列化为string后又将其解析回Time

如果你可以控制日期字段的序列化方式,你可以使用以下格式,与你提供给Jackson的ObjectMapper保持一致:

now := time.Now()
formattedDate := now.Format("2006-01-02T15:04:05.000Z0700")

如果你无法控制Go端的日期序列化方式,你也可以在Java端调整日期格式。以下示例假设Go使用了time.RFC3339

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
英文:

The Go code you provided will not impact the way how the Time instance will be serialized as you are parsing it back into Time after serializing it to a string.

If you have control over how your date fields are serialized, you can apply the following format that should be aligned with what you provided to Jackson's ObjectMapper:

now := time.Now()
formattedDate := now.Format("2006-01-02T15:04:05.000Z0700")

If you don't have control over how the date is serialized on the Go side, you could also adjust the date format on the Java side. The following example assumes that time.RFC3339 is used by Go:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

huangapple
  • 本文由 发表于 2021年6月11日 13:30:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/67931707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定