spring springboot 配置swagger

总结

knife4j 这个玩意儿封装了swagger ,所以配置更简单!除了单体,微服务架构也可以使用!

配置文件



@EnableSwagger2
@EnableKnife4j
@Configuration
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig   {
    @Bean
    public Docket createRestApi() {


//        List<Parameter> pars = new ArrayList<Parameter>();
//
//        //构建默认参数
//        ParameterBuilder ticketPar = new ParameterBuilder();
//        ticketPar.name("Content-Type").description("Content-Type")
//                .modelRef(new ModelRef("string")).parameterType("header")
//                .required(true)
//                .defaultValue("application/json").build();
//        pars.add(ticketPar.build());
//        //构建默认请求验证参数
//        ticketPar = new ParameterBuilder();
//        ticketPar.name("Authorization").description("Authorization")
//                .modelRef(new ModelRef("string")).parameterType("header")
//                .required(true)
//                .defaultValue("111111111111111111111111111111111111111111111111").build();
//        pars.add(ticketPar.build());


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
                //使用上面的默认参数.build().globalOperationParameters(pars);
    }

    //构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("功能测试")
                //创建人
                .contact(new Contact("author", "url", "email"))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }

}

依赖

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <flink.version>1.10.1</flink.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
        <spring-boot.version>2.3.9.RELEASE</spring-boot.version>
        <alibaba-cloud.version>2.2.1.RELEASE</alibaba-cloud.version>
        <nacos.version>0.2.2.RELEASE</nacos.version>
        <mysql-connector.version>5.1.37</mysql-connector.version>
        <mybatis-plus.version>3.2.0</mybatis-plus.version>
        <alibaba-fastjson.version>1.2.70</alibaba-fastjson.version>
        <alibaba-nacos-discovery.version>2.2.1.RELEASE</alibaba-nacos-discovery.version>
        <spring-boot-admin.version>2.2.4</spring-boot-admin.version>
        <knife4j.version>2.0.2</knife4j.version>

    </properties>


  <!-- knife4j -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>${knife4j.version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-micro-spring-boot-starter</artifactId>
            <version>${knife4j.version}</version>
        </dependency>

@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")

这个是主要解决,可以在yml中配置是否启用swagger的

yml

swagger:
  enable: true

Spring Cloud微服务架构中使用

在Spring Cloud的微服务架构下,每个微服务其实并不需要引入前端的Ui资源,因此在每个微服务的Spring Boot项目下,引入knife4j提供的微服务starter

<dependency>    
  <groupId>com.github.xiaoymin</groupId>    
  <artifactId>knife4j-micro-spring-boot-starter</artifactId>    
  <version>${knife4j.version}</version>
</dependency>

在网关聚合文档服务下,可以再把前端的ui资源引入

<dependency>    
   <groupId>com.github.xiaoymin</groupId>    
   <artifactId>knife4j-spring-boot-starter</artifactId>    
   <version>${knife4j.version}</version>
</dependency>

上图

参考

https://blog.csdn.net/xiaoxiaole0313/article/details/104935547

问题

网关搞聚合没有成功!

但可以网关地址加服务地址访问文档 此处网关是8000 hm-demo是8801
http://127.0.0.1:8000/hm-demo1/doc.html#/home

posted @ 2022-02-26 23:45  方东信  阅读(1291)  评论(0编辑  收藏  举报