为Java LocalDateTime生成正确的swagger规范

时间:2019-04-07 04:58:12

标签: java swagger

我正在尝试在Spring Boot应用程序中使用Swagger注释从Java代码中获取swagger spec(yaml)。我注释了模型,然后运行springboot应用程序,然后从http://localhost:8080/v2/api-docs获取规范。

我的模型如下所示:

class Foo(object):
    def __init__(self):
        super(Foo, self).__init__()

我想为此创建正确的摇摇欲坠的Yaml斑点。当我访问自动提供yaml的swagger UI时,我期望以下内容。

请注意,dateOfBirth是Java 8 LocalDateTime类。但是按照大张旗鼓的规范(https://swagger.io/specification/查找数据类型节),yaml中的日期应为:“字符串”,格式为:日期时间。

package com.indiana.core;
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModelProperty;

@lombok.ToString
@lombok.Getter
@lombok.Setter
public class SampleClass{

        @JsonProperty(value = "birth_date_time")
    private LocalDateTime birthDateTime;
....
}'

我现在得到的是:

看起来大张旗鼓正在建立一个详细的定义LocalDateTime,这不是我正在寻找的。

 'SampleClass:
    properties:
      dateOfBirth:
        type: string
        format: date-time'

我尝试过

'"definitions": {
        "LocalDateTime": {
            "type": "object",
            "properties": {
                "chronology": {
                    "$ref": "#/definitions/Chronology"
                },
                "dayOfMonth": {
                    "type": "integer",
                    "format": "int32"
                },
                "dayOfWeek": {
                    "type": "string",
                    "enum": ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"]
                },
                "dayOfYear": {
                    "type": "integer",
                    "format": "int32"
                },
                "hour": {
                    "type": "integer",
                    "format": "int32"
                },
                "minute": {
                    "type": "integer",
                    "format": "int32"
                },
                "month": {
                    "type": "string",
                    "enum": ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"]
                },
                "monthValue": {
                    "type": "integer",
                    "format": "int32"
                },
                "nano": {
                    "type": "integer",
                    "format": "int32"
                },
                "second": {
                    "type": "integer",
                    "format": "int32"
                },
                "year": {
                    "type": "integer",
                    "format": "int32"
                }
            }
        },

在A的这些元素中,仅在yaml中将元素定义为字符串,缺少“格式:日期时间”,如下所示。

'A. @ApiModelProperty(dataType = "java.lang.String", example = "17-03-2019 22:18:59", notes = "Birthdaytime desc")
B. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy hh:mm:ss")'

为此需要进行哪些调整才能创建完美的swegger? 我需要这个

 SampleClass:
    properties:
      dateOfBirth:
        type: string

1 个答案:

答案 0 :(得分:2)

只需在您的 LocalDateTime 字段上放置以下注释,即可在 swagger 定义中格式化日期时间:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = Shape.STRING)
@ApiModelProperty(required = true, example = "2021-08-20T00:00:00")