首页 > 编程语言 > 详细

Springboot集成Swagger2

时间:2020-04-13 19:23:39      阅读:55      评论:0      收藏:0      [点我收藏+]

Swagger2简介

1.随项目自动生成强大RESTful API文档,减少工作量
2.API文档与代码整合在一起,便于同步更新API说明
3.页面测试功能来调试每个RESTful API

springboot集成Swagger2步骤

1. 新建一个Springboot项目

技术分享图片

2. 导入依赖

技术分享图片

		  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		 <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>

3. 编写控制器

技术分享图片

@RestController
public class Mycontroller {

    @GetMapping({"/","hello"})
    public String hello(){
        return "helloword";
    }
}

4. 编写swagger的配置类

写上注解
@Configuration
@EnableSwagger2
技术分享图片

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

启动项目运行
浏览器输入 http://localhost:8080/swagger-ui.html
技术分享图片
我们所写的controller方法都会被检测到

5. 配置Swagger2

  1. 配置Swagger信息 info
    技术分享图片
    @Bean
    public Docket docket() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo());
    }
    public ApiInfo apinfo(){
        Contact contact = new Contact(
                "joker_dj",
                "https://www.cnblogs.com/joker-dj/",
                "2332810801@qq.com");
        return new ApiInfo("joker_dj的api文档",
                "Api Documentation",
                "1.0",
                "https://www.cnblogs.com/joker-dj/",
                contact, "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

运行看效果

技术分享图片
2. 配置多个分组
多配置几个Docket即可
技术分享图片
技术分享图片
3. 配置其他的信息 不一一解释 都有注释
技术分享图片

@Bean
    public Docket docket(){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apinfo())
                //是否启用swagger false不启用
                .enable(true)
                .groupName("joker_dj")
                .select()
                /*
                配置RequestHandlerSelectors 扫描接口的方式
                指定扫描的包basePackage
                any()扫描全部
                none()不扫描
                withClassAnnotation:扫描类上的注解
                */
                .apis(RequestHandlerSelectors.basePackage("com.dj.controller"))
                //过滤路径 比如PathSelectors.ant("login/**")
               // .paths(PathSelectors.ant(""))
                .build();
    }

6.添加注释信息

  1. 编写user实体
    技术分享图片
  2. 在controller返回一个user
    技术分享图片
    运行
    技术分享图片
    会检测到我们所有写的controller方法
    也可以进行测试方法
    点击Try it out
    技术分享图片
  3. 点击运行
    技术分享图片
    这里就会返回我们测试接口的信息以及响应的结果
    技术分享图片
  4. 给类,属性和方法添加注释信息
    在user实体类添加注解
    @ApiModel:给类添加注释
    @ApiModelProperty :给字段添加注释
    技术分享图片
@ApiModel("用户实体类")
public class user {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

运行看效果
技术分享图片
添加到controller上
@ApiOperation
技术分享图片
运行看效果
技术分享图片
5. 发送数据 测试接口
在controller中编写代码
技术分享图片
运行测试
技术分享图片
输入用户名 密码 点击测试
技术分享图片
技术分享图片
这样就返回了我们数据

总结:Swagger这个工具主要是针对开发人员测试接口来使用的,
注意:在项目上线的时候己得吧Swagger给关闭 防止接口暴露 (安全)

Springboot集成Swagger2

原文:https://www.cnblogs.com/joker-dj/p/12693051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!