SpringBoot-swagger

13. SpringBoot-swagger

13.1 导入相关依赖

<dependencies>

    <!-- springfox-boot-starter
        配置类上使用的开启注解为:@EnableOpenApi
        访问路径:http://localhost:8080/swagger-ui/index.html
    -->
    <!--<dependency>-->
    <!--<groupId>io.springfox</groupId>-->
    <!--<artifactId>springfox-boot-starter</artifactId>-->
    <!--<version>3.0.0</version>-->
    <!--</dependency>-->

    <!-- springfox-swagger-ui springfox-swagger2
        springboot2.6.0版本不兼容 这里使用的是SpringBoot2.5.6版本
        使用的注解为:@EnableSwagger2
        访问路径:http://localhost:8080/swagger-ui.html
     -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <!-- springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

13.2 编写Swagger配置类

文件路径:com--dzj-config--SwaggerConfig.java

@Configuration
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {
    // 配置swagger Docket的bean实例,可以配置多个
    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles()判断当前环境是否处于设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println("当前测试环境--->" + flag);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag) //是否启动Swagger,默认为true
                .groupName("dengzj")
                .select()
                /*
                    RequestHandlerSelectors,配置要扫描接口的方式
                    basePackage("com.dzj.controller"):指定要扫描的包
                    any():扫描所有
                    none():不扫描
                    withClassAnnotation(GetMapping.class):扫描类上的注解
                    withMethodAnnotation(GetMapping.class):扫描方法上的注解
                 */
                .apis(RequestHandlerSelectors.basePackage("com.dzj.controller"))
                //paths(PathSelectors.ant("/dzj/**")) 过滤请求的路径
                //.paths(PathSelectors.ant("/hello/**"))
                .build();
    }

    //配置Swagger信息,apiInfo
    private ApiInfo apiInfo() {
        Contact contact = new Contact("dengzj", "https://www.cnblogs.com/aadzj/", "aadzj@qq.com");
        return new ApiInfo(
                "Dengzj的SwaggerApi文档",
                "认真却不怂",
                "v1.0",
                "https://www.cnblogs.com/aadzj/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }
}

13.3 编写实体类

文件位置:com--dzj--pojo--User.java

@ApiModel("用户实体类")  //注释
public class User {

    @ApiModelProperty("用户名") //注释
    public String username;
    @ApiModelProperty("密码")  //注释
    public String password;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

13.4 编写测试接口

文件位置:com--dzj--controller-HelloController.java

@Api(tags = "hello控制类")  //注释类名
@RestController
public class HelloController {

    @ApiOperation("hello接口")  //注释接口名
    @GetMapping(value = "/testHello")
    public String testHello() {
        return "hello";
    }

    // 只要我们接口中的返回值有实体类,它就会被扫描到swagger中
    @ApiOperation("扫描实体类测试接口")
    @PostMapping("/testUser")
    public User testUser() {
        return new User();
    }

    @ApiOperation("测试testRestful接口")
    @GetMapping("/testRestful/{username}")
    public String testRestful(@ApiParam("用户名") @PathVariable("username") String username) {
        return "username:" + username;
    }

    @ApiOperation("测试testPost接口")
    @PostMapping("/testPost")
    public User testPost(User user) {
        return user;
    }
}

13.5 编写配置文件

  • application.properties

    spring.profiles.active=dev
    
  • application-dev.properties

  • application-pro.properties

  • application-test.properties

13.6 访问测试

访问路径:http://localhost:8080/swagger-ui.html

接口以及实体类信息

posted @ 2021-12-16 09:15  小公羊  阅读(176)  评论(0编辑  收藏  举报