我想使用 swagger 合约创建一个包含createdDate feild 类型作为 LocalDateTime 的模型类,当使用 swagger 合约实现时,它会生成 offsetDateTime 作为 dto 中的类型。
尝试过这种方法在招摇中添加了详细信息:
createdDate:
type: string
format: date-time
example: 2017-01-13T17:09:42.411
请为此场景提供正确的 swagger 实现。
在带有 Spring Boot 的 Java 中,您可以将 @JsonSerialize 和 @JsonDeserialize 注释与自定义转换器一起使用:
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
public class YourModelClass {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime createdDate;
// getters and setters
}
然后,您需要定义实现序列化和反序列化逻辑的 LocalDateTimeSerializer 和 LocalDateTimeDeserializer 类。