前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 集成 Swagger,再也不写接口文档了!

Spring Boot 集成 Swagger,再也不写接口文档了!

作者头像
Java技术栈
发布2019-07-08 12:06:44
4960
发布2019-07-08 12:06:44
举报
文章被收录于专栏:Java技术栈Java技术栈

今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单。

Spring Boot 集成 Swagger

1、添加依赖

Maven依赖示例:

代码语言:javascript
复制
<!-- 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 配置文件中添加配置参数。
代码语言:javascript
复制
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@javastack.cn
3、添加配置类
代码语言:javascript
复制
@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 描述和参数信息,但这样不是很直观,需要对每个接口和参数进行自定义描述。

常用的 Swagger 注解如下。

注解名称

使用说明

@Api

描述一个 API 类

@ApiImplicitParam

描述一个请求参数

@ApiImplicitParams

描述一组请求参数

@ApiModel

描述一个返回的对象

@ApiModelProperty

描述一个返回的对象参数

@ApiOperation

描述一个 API 方法

@ApiParam

描述一个方法的参数

@ApiResponse

描述一个请求响应

@ApiResponses

描述一组请求响应

使用示例如:

代码语言:javascript
复制
@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

打开 swagger-ui 界面,可以看到所有的 API 接口定义,也可以在上面发起接口测试。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-03-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java技术栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Boot 集成 Swagger
    • 1、添加依赖
      • 2、在 Spring Boot 配置文件中添加配置参数。
        • 3、添加配置类
        • 如何使用
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档