我们可以使用 swagger-generator 在模型变量上生成 @JsonFormat 吗? [英] Can we generate @JsonFormat on model variable using swagger-generater?
问题描述
我在 yml 文件中有一个变量
I have a variable in yml file
startDate:
type:string
format:date
我正在使用 swagger-generator 从 yml 生成 java 代码.
I'm using swagger-generater to generate java code from yml.
它正在生成一个 startDate 变量,如下所示
It's generating a startDate variable as below
@JsonProperty("startDate")
private LocalDate startDate = null;
但我需要如下
@JsonProperty("startDate")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate startDate = null;
有人可以帮助我吗?
推荐答案
这里的问题是您正在尝试使用 @JsonFormat 序列化
Java 8
LocalDate
不使用正确的 jackson 模块/依赖项.如果您查看注释文档,它会说;
The problem here is you are trying to serialize a Java 8
LocalDate
using @JsonFormat
without using right jackson module/dependency. If you have a look the annotation doc, it says;
常见用途包括在替代表示之间进行选择——用于例如,日期是否要序列化为数字(Java 时间戳)或字符串(例如 ISO-8601 兼容时间值)——以及使用 pattern() 属性配置确切的细节.
Common uses include choosing between alternate representations -- for example, whether Date is to be serialized as number (Java timestamp) or String (such as ISO-8601 compatible time value) -- as well as configuring exact details with pattern() property.
swagger codegen
项目中没有关于如何指定特定于语言的配置选项的适当文档,我只在以下票证中看到了这些配置选项;
There's no proper documentation in the swagger codegen
project about how to specify language specific configuration options, I have only seen those config options in the following ticket;
https://github.com/swagger-api/swagger-codegen/issues/7795
根据那个问题,您可以强制 Swagger 代码生成器使用 java8
dateLibrary
.
As per the that issue, you could force Swagger codegen to use java8
dateLibrary
.
这篇关于我们可以使用 swagger-generator 在模型变量上生成 @JsonFormat 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!