springboot整合swagger实现接口文档

本文档介绍了如何在后端开发中集成Swagger,通过自动化的API文档生成,提升接口文档的质量和效率。首先,引入Swagger的相关依赖,然后配置Swagger的Bean,并在Controller类上添加注解以描述接口。启动项目后,访问特定URL即可查看Swagger UI,方便测试和查阅接口信息。常见注解如@Api、@ApiOperation等用于接口和方法的详细说明。
摘要由CSDN通过智能技术生成

作为后端程序员,有时候很烦自己接口文档写的不好,不如学一学项目中整合swagger,让程序帮你写。

1.导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.编写配置文件


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 //开启Swagger2
@ComponentScan("com.summer.controller")//新增
public class SwaggerConfig {
//    //多个Docket实现多组
//    @Bean
//    public Docket docket1(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("A")
//                .select()
//                .apis(RequestHandlerSelectors.none())
//                .build();
//    }
//    @Bean
//    public Docket docket2(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("B")
//                .select()
//                .apis(RequestHandlerSelectors.none())
//                .build();
//    }
//    @Bean
//    public Docket docket3(){
//        return new Docket(DocumentationType.SWAGGER_2).groupName("C")
//                .select()
//                .apis(RequestHandlerSelectors.none())
//                .build();
//    }



    //配置了Swagger的Bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger环境
        Profiles prfiles= Profiles.of("dev","test");
        //通过environment.acceptsProfiles判断是否出处在自己设定的环境当中
        boolean flag=environment.acceptsProfiles(prfiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("xxx")
                .enable(true)//enable是否启动Swagger,如果为false不启动,则不能在浏览器里访问Swagger如果为true启动Swagger
                .select()
                //RequestHandlerSelectors 扫描要配置接口的方式
                //basePackage 指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.summer.controller"))
                //any():扫描全部
                //none():全不扫描
                //withClassAnnotation:扫描类上的注解
                //withMethodAnnotation:扫描方法上的注解
                //.apis(RequestHandlerSelectors.any())
                //paths():过滤路径
               // .paths(PathSelectors.ant("/controller/"))
                .build();
    }
    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact=new Contact("Teamwork","http://www.apache.org/licenses/LICENSE-2.0","");
        return new ApiInfo("xxx的xxx文档",
                "xxx",
                "v1.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

3.接着在controller类上加上几个注解,以下是一个demo例子

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(value = "TbRoleController", description = "权限api")
public class TbRoleController {
    @Autowired(required =false)
    private TbRoleMapper tbRoleMapper;
    @ApiOperation("测试swagger")
    @PostMapping("/aa/bb")
    @ResponseBody
    public String addaa( ) throws IOException, TokenExpiredException {
  	return "hello world";
    }

4.最后在启动项目输入地址跳转到swagger的测试地址

地址为: htttp://ip地址:端口号/swagger-ui.html

5.常用注解

Swagger的所有注解定义在io.swagger.annotations包下
下面列一些经常用到的,未列举出来的可以另行查阅说明:

Swagger注解简单说明
@Api(tags = “xxx模块说明”)作用在模块类上
@ApiOperation(“xxx接口说明”)作用在接口方法上
@ApiModel(“xxxPOJO说明”)作用在模型类上:如VO、BO
@ApiModelProperty(value = “xxx属性说明”,hidden = true)作用在类方法和属性上,hidden设置为true可以隐藏该属性
@ApiParam(“xxx参数说明”)作用在参数、方法和字段上,类似@ApiModelProperty

6.springmvc配置swagger

<!--添加swagger配置-->
<!-- 引入swagger相关 -->
<bean class="com.java5678.config.MySwagger"/>
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
    <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
<!--其他的跟springboot一样(注意在springmvc的配置文件applicationContext.xml里弄)-->

7.参考资料

b站:狂神说java
https://www.bilibili.com/video/BV1Y441197Lw

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Summer524!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值