Cannot deserialize value of type `java.time.OffsetDateTime` from String in openapi client

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

Cannot deserialize value of type `java.time.OffsetDateTime` from String in openapi client

问题

  1. 我有一个使用Spring Boot的应用程序,其中包含通过Gradle插件生成的Java客户端:
  2. ```groovy
  3. openApiGenerate {
  4. generatorName = "java"
  5. inputSpec = specsYml
  6. outputDir = "$buildDir/generated".toString()
  7. apiPackage = "com.customapi.api"
  8. invokerPackage = "com.customapi.invoker"
  9. modelPackage = "com.customapi.model"
  10. configOptions = [
  11. dateLibrary: "java8",
  12. library : "resttemplate"
  13. ]
  14. }

我选择了"java8"作为dateLibrary,因为这似乎是适用于使用Java 1.8的项目的首选库。

使用生成的客户端,我执行了一个请求,返回一个包含时间戳的对象。我收到以下错误:

  1. java.lang.IllegalStateException: Failed to execute CommandLineRunner
  2. ...
  3. Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.customapi.model.Info] and content type [application/json];
  4. ...
  5. Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-07-21T12:12:23.000+0200": ...
  6. ...
  7. ...
  8. Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-07-21T12:12:23.000+0200": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-07-21T12:12:23.000+0200' could not be parsed at index 23
  9. at [Source: (ByteArrayInputStream); line: 1, column: 84] (through reference chain: com.customapi.model.Info["buildTimestamp"])
  10. at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67) ~[jackson-databind-2.10.3.jar:2.10.3]
  11. at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1679) ~[jackson-databind-2.10.3.jar:2.10.3]
  12. at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:935) ~[jackson-databind-2.10.3.jar:2.10.3]
  13. at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:86) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  14. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:218) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  15. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:50) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  16. at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.10.3.jar:2.10.3]
  17. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369) ~[jackson-databind-2.10.3.jar:2.10.3]
  18. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.10.3.jar:2.10.3]
  19. at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4218) ~[jackson-databind-2.10.3.jar:2.10.3]
  20. at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3267) ~[jackson-databind-2.10.3.jar:2.10.3]
  21. at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:269) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  22. ... 17 common frames omitted
  23. Caused by: java.time.format.DateTimeParseException: Text '2020-07-21T12:12:23.000+0200' could not be parsed at index 23
  24. at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_151]
  25. at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777) ~[na:1.8.0_151]
  26. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:212) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  27. ... 24 common frames omitted

涉及到问题的Info类的相关部分:

  1. ...
  2. @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-07-26T14:09:54.137+02:00[Europe/Berlin]")
  3. public class Info {
  4. ...
  5. public static final String JASON_PROPERTY_BUILD_TIMESTAMP = "buildTimestamp";
  6. private OffsetDateTime buildTimestamp;
  7. ...
  8. public Info buildTimestamp(OffsetDateTime buildTimestamp) {
  9. this.buildTimestamp = buildTimestamp;
  10. return this;
  11. }
  12. public void setBuildTimestamp(OffsetDateTime buildTimestamp) {
  13. this.buildTimestamp = buildTimestamp;
  14. }
  15. ...
  16. }

两个setter方法都接受OffsetDateTime对象,并且没有注释,因此转换必须在其他地方进行。输入字符串再次为"2020-07-21T12:12:23.000+0200"。

相关的依赖项是:

  1. ext {
  2. swagger_annotations_version = "1.5.22"
  3. jackson_version = "2.10.3"
  4. jackson_databind_version = "2.10.3"
  5. jackson_databind_nullable_version = "0.2.1"
  6. }
  7. dependencies {
  8. compile "io.swagger:swagger-annotations:$swagger_annotations_version"
  9. compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
  10. compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
  11. compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
  12. compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jackson_version"
  13. compile "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
  14. compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
  15. }

似乎有很多关于Jackson和Java 8的问题,这个网站上的大多数解决方案似乎都是添加注释。但我怀

英文:

I have a spring boot application with a java client generated via the gradle plugin:

  1. openApiGenerate {
  2. generatorName = "java"
  3. inputSpec = specsYml
  4. outputDir = "$buildDir/generated".toString()
  5. apiPackage = "com.customapi.api"
  6. invokerPackage = "com.customapi.invoker"
  7. modelPackage = "com.customapi.model"
  8. configOptions = [
  9. dateLibrary: "java8",
  10. library : "resttemplate"
  11. ]
  12. }

I have chosen "java8" as dateLibrary as that seems to be the preferred one for a project with java 1.8.

With that generated client I'm executing a request that returns an object that holds a timestamp.
I get the following error:

  1. java.lang.IllegalStateException: Failed to execute CommandLineRunner
  2. ...
  3. Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.customapi.model.Info] and content type [application/json];
  4. ...
  5. Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-07-21T12:12:23.000+0200": ...
  6. ...
  7. ...
  8. Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.OffsetDateTime` from String "2020-07-21T12:12:23.000+0200": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2020-07-21T12:12:23.000+0200' could not be parsed at index 23
  9. at [Source: (ByteArrayInputStream); line: 1, column: 84] (through reference chain: com.customapi.model.Info["buildTimestamp"])
  10. at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67) ~[jackson-databind-2.10.3.jar:2.10.3]
  11. at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1679) ~[jackson-databind-2.10.3.jar:2.10.3]
  12. at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:935) ~[jackson-databind-2.10.3.jar:2.10.3]
  13. at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:86) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  14. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:218) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  15. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:50) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  16. at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.10.3.jar:2.10.3]
  17. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369) ~[jackson-databind-2.10.3.jar:2.10.3]
  18. at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.10.3.jar:2.10.3]
  19. at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4218) ~[jackson-databind-2.10.3.jar:2.10.3]
  20. at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3267) ~[jackson-databind-2.10.3.jar:2.10.3]
  21. at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:269) ~[spring-web-5.2.7.RELEASE.jar:5.2.7.RELEASE]
  22. ... 17 common frames omitted
  23. Caused by: java.time.format.DateTimeParseException: Text '2020-07-21T12:12:23.000+0200' could not be parsed at index 23
  24. at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_151]
  25. at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777) ~[na:1.8.0_151]
  26. at com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.deserialize(InstantDeserializer.java:212) ~[jackson-datatype-jsr310-2.10.3.jar:2.10.3]
  27. ... 24 common frames omitted

The relevant parts of the Info class in question:

  1. ...
  2. @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-07-26T14:09:54.137+02:00[Europe/Berlin]")
  3. public class Info {
  4. ...
  5. public static final String JASON_PROPERTY_BUILD_TIMESTAMP = "buildTimestamp";
  6. private OffsetDateTime buildTimestamp;
  7. ...
  8. public Info buildTimestamp(OffsetDateTime buildTimestamp) {
  9. this.buildTimestamp = buildTimestamp;
  10. return this;
  11. }
  12. public void setBuildTimestamp(OffsetDateTime buildTimestamp) {
  13. this.buildTimestamp = buildTimestamp;
  14. }
  15. ...
  16. }

Both setter methods accept OffsetDateTime objects and have no annotations so the conversion must happen elsewhere. The input String again is "2020-07-21T12:12:23.000+0200".
relevant dependencies are

  1. ext {
  2. swagger_annotations_version = "1.5.22"
  3. jackson_version = "2.10.3"
  4. jackson_databind_version = "2.10.3"
  5. jackson_databind_nullable_version = "0.2.1"
  6. }
  7. dependencies {
  8. compile "io.swagger:swagger-annotations:$swagger_annotations_version"
  9. compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
  10. compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
  11. compile "com.fasterxml.jackson.core:jackson-databind:$jackson_databind_version"
  12. compile "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jackson_version"
  13. compile "org.openapitools:jackson-databind-nullable:$jackson_databind_nullable_version"
  14. compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson_version"
  15. }

There seem to be a lot of problems with jackson and java 8 and most solutions on this site seem to be adding annotations. But I doubt that modifying generated code is the proper solution. Did I overlook an important parameter when generating the client? Does the server supply the wrong format? How can I investigate this?

Update:

When I switch dateLibrary to legacy it works, so I think I receive the correct data.

There is a bug in the (jaxrs) server generator https://github.com/swagger-api/swagger-codegen/issues/3648#issuecomment-244056314 that has the server send out wrongly formatted (without colon) date-time. My solution was to use the legacy dateLibrary for the client which can handle the wrong format.

答案1

得分: 10

在问题的评论中,我意识到您不需要使用Jackson注解。您只需要调整setter方法。以下是一个基本示例:

假设有以下类:

  1. import java.time.OffsetDateTime;
  2. //import com.fasterxml.jackson.annotation.JsonSetter;
  3. import java.time.format.DateTimeFormatter;
  4. public class MyOdt {
  5. private OffsetDateTime odt;
  6. public OffsetDateTime getOdt() {
  7. return odt;
  8. }
  9. //@JsonSetter("odt")
  10. public void setOdt(String odtString) {
  11. final String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSxx";
  12. DateTimeFormatter dtfB = DateTimeFormatter.ofPattern(pattern);
  13. this.odt = OffsetDateTime.parse(odtString, dtfB);
  14. }
  15. }

该类将从以下类似的JSON片段创建:

  1. String jsonTest = "{\"odt\" : \"2020-07-21T12:12:23.000+0200\"}";

对象映射器:

  1. ObjectMapper objectMapper = new ObjectMapper()
  2. .registerModule(new JavaTimeModule())
  3. .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  4. MyOdt odtTest = objectMapper.readValue(jsonTest, MyOdt.class);

供参考,以下是问题中的原始评论:

> 一个观察:这不是OffsetDateTime.parse()可以解析的有效字符串,因为默认的日期时间格式期望偏移量中有一个冒号:+02:00。因此,以下格式是有效的:OffsetDateTime.parse("2020-07-21T12:12:23.000+02:00")

英文:

Following my comment in the question, I realize you don't need a Jackson annotation. You just need to adjust the setter. Here is a basic demo:

Assume the following class:

  1. import java.time.OffsetDateTime;
  2. //import com.fasterxml.jackson.annotation.JsonSetter;
  3. import java.time.format.DateTimeFormatter;
  4. public class MyOdt {
  5. private OffsetDateTime odt;
  6. public OffsetDateTime getOdt() {
  7. return odt;
  8. }
  9. //@JsonSetter("odt")
  10. public void setOdt(String odtString) {
  11. final String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSxx";
  12. DateTimeFormatter dtfB = DateTimeFormatter.ofPattern(pattern);
  13. this.odt = OffsetDateTime.parse(odtString, dtfB);
  14. }
  15. }

The class will be created from a JSON fragment such as this:

  1. String jsonTest = "{ \"odt\" : \"2020-07-21T12:12:23.000+0200\" }";

The object mapper:

  1. ObjectMapper objectMapper = new ObjectMapper()
  2. .registerModule(new JavaTimeModule())
  3. .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  4. MyOdt odtTest = objectMapper.readValue(jsonTest, MyOdt.class);

For reference, here is the original comment in the question:

> An observation: That is not a valid string to be parsed by OffsetDateTime.parse() because the default datetime format expects the offset to have a colon in it: +02:00. So, this works: OffsetDateTime.parse("2020-07-21T12:12:23.000+02:00")

答案2

得分: 2

我通过将 dateLibrary 切换为 java8-localdatetime 得到了帮助。

另请参阅这里

英文:

I was helped by switching dateLibrary to java8-localdatetime

See also here.

huangapple
  • 本文由 发表于 2020年7月27日 18:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63113331.html
匿名

发表评论

匿名网友

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

确定