Swagger2使用

一  引入依赖

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

二 编写配置类

package com.yoocar.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2//启用Swagger2
public class SwaggerConfig {

    //http://ip:port/swagger-ui.html   项目路径/swagger-ui.html
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")//配置映射路径和要扫描的接口的位置
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lxd"))//配置要扫描的接口的位置
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()//配置一下Swagger2文档网站的信息
                        .title("lxd接口文档")//网站的title
                        .description("本站是lxd科技的接口文档信息")//网站的描述
                        .version("9.0")//接口版本
                        .contact(new Contact("开发部","http://www.yoka.com/","xxx@gmail.com"))//联系人的信息
                        .license("The Apache License")//使用的协议
                        .licenseUrl("http://www.baidu.com")//
                        .build());
    }

}

 三 controller 层加注解 

@Api(tags = "系统产品管理界面")
上一篇:swagger


下一篇:超简单配置接口文档Swagger和Knife4j教程