Swagger-时间戳奇怪的表示

Son*_*que 2 swagger spring-boot

我有Spring Boot控制器,为了方便使用Swagger,可以参考API。有关的问题timestamp。为什么在生成的文档示例中如下所示:

"timestamp": { "date": 0, "day": 0, "hours": 0, "minutes": 0, "month": 0, "nanos": 0, "seconds": 0, "time": 0, "timezoneOffset": 0, "year": 0 }

那么,为什么昂首阔步有这么奇怪的时间戳示例,而不仅仅是简单的数值,哪个标准描述了这种表示形式?

Saf*_*azi 5

检查Swagger的开放规范(https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)。

如果您想解决该问题,请尝试以下示例Spring Boot应用程序

import java.io.IOException;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@RestController
@EnableSwagger2
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping(value = "/api", method = RequestMethod.POST)
    public Demo messages(HttpServletRequest request, HttpServletResponse response) throws IOException {
        java.util.Date date = new java.util.Date();
        return new Demo(new Timestamp(date.getTime()));
    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage(getClass().getPackage().getName())).paths(PathSelectors.any())
                .build().apiInfo(generateApiInfo()).directModelSubstitute(Timestamp.class, Long.class);
    }

    private ApiInfo generateApiInfo() {
        return new ApiInfo("demo", "demo.", "Version 1.0", "urn:tos", "test", "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0");
    }
}
Run Code Online (Sandbox Code Playgroud)

演示对象

import java.sql.Timestamp;

public class Demo {
private Timestamp time;

public Timestamp getTime() {
    return time;
}

public void setTime(Timestamp time) {
    this.time = time;
}

public Demo(Timestamp time) {
    super();
    this.time = time;
}

public Demo() {
    super();
}
Run Code Online (Sandbox Code Playgroud)

}

检查下图或打开http:// localhost:8080 / swagger-ui.html

在此处输入图片说明

.directModelSubstitute(Timestamp.class, Long.class) 负责时间戳类型和数字类型之间的映射