springboot整合swagger

来源:互联网 发布:mysql数据库命令行 编辑:程序博客网 时间:2024/06/05 08:24

添加依赖

<dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger2</artifactId>    <version>2.4.0</version></dependency><dependency>    <groupId>io.springfox</groupId>    <artifactId>springfox-swagger-ui</artifactId>    <version>2.4.0</version></dependency>

配置Swagger

@SpringBootApplication@EnableScheduling@ServletComponentScanpublic class DemoApplication {    //应用入口    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }    //swagger的配置     @Bean        public Docket demoApi() {            return new Docket(DocumentationType.SWAGGER_2)                    .groupName("演示环境")                    .genericModelSubstitutes(DeferredResult.class)                    //  .genericModelSubstitutes(ResponseEntity.class)                    .useDefaultResponseMessages(false)                    .forCodeGeneration(false)                    .pathMapping("/")                    .select()                    .paths(PathSelectors.regex("/common.*"))//过滤的接口                    .build()                    .apiInfo(demoApiInfo());//api介绍设置        }     //API介绍信息     private ApiInfo demoApiInfo() {            Contact contact = new Contact("qiming", "http://baidu.com", "2193714269@qq.com");            ApiInfo apiInfo = new ApiInfo("测试接口",//大标题                    "REST风格API",//小标题                    "0.1",//版本                    "www.baidu.com",                    contact,//作者                    "主页",//链接显示文字                    ""//网站链接            );            return apiInfo;        }}

访问接口文档

http://localhost/swagger-ui.html

一些问题

接口的返回的数据得足够清晰,太多干扰信息,可能会导致接口使用者无法甄别哪些是有用的数据。

0 0