springboot引入swagger-ui

1.依赖
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>
2.配置 application.yml

# swagger 开关
swagger:
  enable: true

3. 配置类 Swagger2

点击查看代码
import org.springframework.beans.factory.annotation.Value;
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.ApiInfo;
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
public class Swagger2 {

    // swagger开关
    @Value("${swagger.enable}")
    private boolean swaggerEnable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerEnable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.pingan.controller"))    // swagger扫描指定包下面的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("ex-laiqiaobo001","http://127.0.0.1:8001(随便或者网站地址)","ex-laiqiaobo001@qq.com");
        return new ApiInfoBuilder()
                .title("这个是swagger的标题")
                .description("这个是副标题")
                .contact(contact)
                .version("1.0")
                .build();
    }

}

 

4.在Controller类上标注注解

@Api(tags = "学生操作")
@RestController
@RequestMapping("/student")
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);
    @Autowired
    private StudentService studentService;

    @ApiOperation(value="查询用户", notes="查找所有用户")
    @GetMapping("/test")
    public String test() {
        log.info("-----------------这个是好事----------------------------");
        return "aaaaaaaaaaa";
    }
}

 - @Api()用于类;

表示标识这个类是swagger的资源
例: @Api(tags = "学生操作")
- @ApiOperation()用于方法;
表示一个http请求的操作
例: @ApiOperation(value="查询用户", notes="查找所有用户")
- @ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略
 
 
5.访问对应的ip和端口: http://127.0.0.1:xxxx/swagger-ui.html/
 
 
6. 经过上面的步骤后,再引入下面的依赖,则可以访问新的swagger-ui界面
http://127.0.0.1:xxxx/doc.html
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>
 
 
7. 如果希望swagger分组显示,则在配置类加多这两个方法
/**
*    分多个组
*    分组必须加组名,且不能重复
*/
@Bean
public Docket createRestApi2() {
    return new Docket(DocumentationType.SWAGGER_2)
        .enable(swaggerEnable)
        .apiInfo(apiInfo2())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.pingan.controller.file"))    // swagger扫描指定包下面的接口
        .paths(PathSelectors.any())
        .build()
        .groupName("这个是第二组");    // 如果分多个组,必须加组名,且不能重复
}

private ApiInfo apiInfo2() {
    Contact contact = new Contact("ex-laiqiaobo001","http://127.0.0.1:8001(随便或者网站地址)","ex-laiqiaobo001@qq.com");
    return new ApiInfoBuilder()
        .title("这个是swagger的标题22222")
        .description("这个是副标题222222")
        .contact(contact)
        .version("1.02")
        .build();
}
 
 

posted @ 2021-12-06 14:36  得好好活  阅读(363)  评论(0)    收藏  举报