cosmo

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  194 随笔 :: 0 文章 :: 31 评论 :: 197万 阅读

1.添加依赖

复制代码
<!-- Swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
</dependency>
复制代码

2.在 Spring Boot 配置文件中添加配置参数

复制代码
swagger:
  title: API标题
  description: API描述
  version: 1.0
  terms-of-service-url: http://www.javastack.cn/
  base-package: cn.javastack.test.web
  contact:
    name: Javastack
    url: http://www.javastack.cn/
    email: admin@qq.cn
复制代码

3.创建配置类:

  

复制代码
@Getter
@Setter
@Configuration
@EnableSwagger2
@ConditionalOnClass(EnableSwagger2.class)
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {

    /**
     * API接口包路径
     */
    private String basePackage;

    /**
     * API页面标题
     */
    private String title;

    /**
     * API描述
     */
    private String description;

    /**
     * 服务条款地址
     */
    private String termsOfServiceUrl;

    /**
     * 版本号
     */
    private String version;

    /**
     * 联系人
     */
    private Contact contact;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .version(version)
                .contact(contact)
                .build();
    }

}
复制代码

Swagger 默认会根据配置的包,扫描所有接口并生成对应的 API 描述和参数信息,但这样不是很直观,需要对每个接口和参数进行自定义描述

注解名称使用说明
@Api 描述一个 API 类
@ApiImplicitParam 描述一个请求参数
@ApiImplicitParams 描述一组请求参数
@ApiModel 描述一个返回的对象
@ApiModelProperty 描述一个返回的对象参数
@ApiOperation 描述一个 API 方法
@ApiParam 描述一个方法的参数
@ApiResponse 描述一个请求响应
@ApiResponses 描述一组请求响应
复制代码
@Api(description = "登录接口")
@RestController
public class LoginController {

    @ApiOperation(value = "登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query")})
    @PostMapping(value = "/login")
    public Object login(@RequestParam("username") String username, @RequestParam("password") String password) {

        // ...

    }
}
复制代码

访问:http://localhost:8080/swagger-ui.html,可以看到所有的 API 接口定义,也可以在上面发起接口测试

posted on   【cosmo】  阅读(384)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2020-02-20 spring boot集成mybatis 自动生成实体类和mapper文件、Dao层
2020-02-20 Registered driver with driverClassName=oracle.jdbc.driver.OracleDriver was not found
2020-02-20 Oracle中创建序列
2020-02-20 mybatis 在oracle数据库中插入数据时获取自增 sequence序列
2020-02-20 spring boot集成mybatis 出现 nvalid bound statement (not found)
2020-02-20 Springboot 链接Oracle 配置
点击右上角即可分享
微信分享提示