我有Spring Boot控制器,并且使用Swagger进行简单的API参考。
关于RUN
的问题。为什么生成的文档示例如下所示:
timestamp
那么为什么swagger有一个如此奇怪的时间戳示例而不仅仅是简单的数值,哪个标准描述了这种表示?
答案 0 :(得分:3)
检查Swagger的开放式规范(https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)。
如果您想解决此问题,请尝试使用此示例Spring启动应用程序
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");
}
}
演示对象
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();
}
}
检查下方图片或打开http://localhost:8080/swagger-ui.html
.directModelSubstitute(Timestamp.class, Long.class)
负责时间戳类型和数字类型之间的映射