springboot 整合swagger2,轻松入门使用

在开发中提供接口文档有些时候是一件费时费力的事情,但又不得不做,现在使用swagger完全可以轻松的梳理出文档,今天记录一下springboot集成swagger2的入门教程。

首先创建springboot的项目,导入需要的依赖

    <!-- 集成swagger-->
        <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>

开启对swagger2的支持

@Configuration
@EnableSwagger2  //开启对swagger的支持
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerui.controller"))//要扫描的接口
                .paths(PathSelectors.any())
                .build().apiInfo(spiInfo());
    }

    public ApiInfo spiInfo(){
        return new ApiInfoBuilder()
                .title("SpringBoot整合Swagger")//标题
                .description("SpringBoot整合Swagger练习demo")//描述
                .version("9.0")//版本
                .contact(new Contact("测试","blog.csdn.net","aaa@gmail.com"))//练习
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build();
    }
}

现在springboot集成swagger2就已经完成了,启动项目,输入http://localhost:8080/swagger-ui.html,

如果能看到如下页面,就说明已经集成成功

现在写一个接口

import com.example.swaggerui.entity.Department;
import com.example.swaggerui.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2020-07-02 15:42:20
 */
@RestController
@RequestMapping("department")
@Api(tags = "部门接口管理")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    @ApiOperation("查询单个部门信息")
    public Department selectOne(Integer id) {
        return this.departmentService.queryById(id);
    }

}

此时启动项目,登录swagger的页面,就可以清晰的看到此接口的接口信息,本文章制作集成的入门,至于swagger的各种注解的使用和接口的详细使用方式后续会补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值