(一)springboot整合之swagger2(详细)

2年前 (2022) 程序员胖胖胖虎阿
133 0 0

1.新建springboot项目

2.引入maven依赖

<!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version> 2.9.2</version>
        </dependency>
<!--        swagger-ui-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

注意:swagger版本可能和springboot版本有冲突导致项目跑不起来,降低springboot版本即可

3.编写swagger配置类(创建docket  配置apiinfo) swaggerconfig

@Configuration

@EnableSwagger2 //开启swagger2

public class SwaggerConfig {
  
}

其实到这里我们就可以通过http://localhost:8080/swagger-ui.html#/访问swagger文档了,但是我们在工作中会用到swagger的其他配置

package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2           //开启swagger2
public class SwaggerConfig {
    //配置了swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    //配置swagger信息  apiinfo
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("可心的swagger文档")
                .description("我想认真读一本书")
                .version("v3.0")
                .termsOfServiceUrl("https://blog.csdn.net/weixin_58993861?type=blog")
                .contact("程序员")
                .build();

    }
}

(一)springboot整合之swagger2(详细)

注:apiinfo其实就是修改了原有的配置 这是原有的默认的apiinfo配置

(一)springboot整合之swagger2(详细)

 

我们可以再次访问http://localhost:8080/swagger-ui.html#/ 看看和之前有什么变化

4.编写swagger配置类(定义扫描接口 定义项目环境 ) swaggerconfig

package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2           //开启swagger2
public class SwaggerConfig {
    //配置了swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){

        //设置要使用的环境中
        Profiles profiles = Profiles.of("dev");
        //通过方法  判断自己项目所使用的的环境  在不在这里的指定的环境里 如果在就能访问的到 不在就访问不到
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//通过方法  判断自己项目所使用的的环境  在不在这里的指定的环境里 如果在就能访问的到 不在就访问不到
                .select()//选择
                //可以扫描 any none 指定包  指定类  指定方法
                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
//                .paths(PathSelectors.ant("com.example.controller/**")) 过滤不需要扫描的路径
                .build();//创建
    }
    //配置swagger信息  apiinfo
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("可心的swagger文档")
                .description("我想认真读一本书")
                .version("v3.0")
                .termsOfServiceUrl("https://blog.csdn.net/weixin_58993861?type=blog")
                .contact("程序员")
                .build();

    }
}

定义扫描接口:要定义扫描接口 就要选择接口创建 select api build 调用这三个方法就行了

定义项目环境:项目启动扫描的是默认环境,我这里默认环经指向了dev环境,通过方法 判断自己项目所使用的的环境 在不在这里的指定的环境里 如果在就能访问的到 不在就访问不到

5.编写swagger配置类(配置多个docket   对实体类参数等进行中文注释  进行接口的测试) swaggerconfig

配置多个docket:在实际开发中一个项目就一个swagger文档,每个人负责自己的模块所以每个人只需要负责好自己的docket就行了。

这里配置了三个docket

 @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("张三部分");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("李四部分");
    }


    //配置了swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){

        //设置要使用的环境中
        Profiles profiles = Profiles.of("dev");
        //通过方法  判断自己项目所使用的的环境  在不在这里的指定的环境里 如果在就能访问的到 不在就访问不到
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("DerKing部分")
                .apiInfo(apiInfo())
                .enable(flag)//通过方法  判断自己项目所使用的的环境  在不在这里的指定的环境里 如果在就能访问的到 不在就访问不到
                .select()//选择
                //可以扫描 any none 指定包  指定类  指定方法
                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
//                .paths(PathSelectors.ant("com.example.controller/**")) 过滤不需要扫描的路径
                .build();//创建
    }

swagger中选择自己的部分就行了

(一)springboot整合之swagger2(详细)

 对实体类参数等进行中文注释:

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;

}

(一)springboot整合之swagger2(详细)

 

@ApiOperation("写在controller的方法上的")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String username, @ApiParam("密码") String password){
    return password + username;
}
@ApiOperation("写在controller的方法上的")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String username, @ApiParam("密码") String password){
    return password + username;
}

(一)springboot整合之swagger2(详细)

 进行接口的测试:

点击try it out 测试接口就可以了。

版权声明:程序员胖胖胖虎阿 发表于 2022年9月13日 下午3:24。
转载请注明:(一)springboot整合之swagger2(详细) | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...