SpringBoot集成Swagger2,采用Bootstrap-UI界面详细教程,高效整洁的接口文档

只需要三个步骤即可完成springboot集成swagger2,界面使用bootstrap-ui。

1.在pom.xml文件里添加依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

 2,创建swagger配置类:

类上加上三个注解

@Configuration

@EnableSwagger2

@EnableSwaggerBootstrapUI

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //控制器位置
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerbootui.controller"))
                .paths(PathSelectors.any())  //表示扫描所有路径
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui RESTful APIs")    //文档的标题,可自定义
                .description("swagger-bootstrap-ui")     //测试的接口文档,可自定义
                .termsOfServiceUrl("http://localhost:8080/")   //
                .contact("1111@qq.com")  //联系人信息,可自定义
                .version("1.0")   //版本信息,可自定义
                .build();
    }
}

3.在控制器类上加入注解@Api(tags = "接口测试")

同时在每个接口方法上加入注解@ApiOperation("名称自定义")

@Api(tags = "用户接口测试")  //名称可自定义
@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("add")
    @ApiOperation("添加用户")  //名称可自定义
    public String add(User user){
        userService.save(user);
        return "添加成功";
    }

    @GetMapping("findAll")
    @ApiOperation("查找所有用户")  //名称可自定义
    public List<User> findAll(){
        return userService.findAll();
    }

    @PostMapping("delete")
    @ApiOperation("根据id删除用户")  //名称可自定义
    public String delete(Long id){
        userService.deleteById(id);
        return "删除成功";
    }

}

 启动项目后,浏览器输入

http://localhost:8080/doc.html

即可进入swagger-bootstrap-ui界面

至此springboot集成swagger-bootstrap-ui完毕。

感谢观看欢迎评论私聊交流技术疑点难点。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

psvm_code

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

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

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

打赏作者

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

抵扣说明:

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

余额充值