• springboot配置swagger2线上文档


    1、先上项目配置好的swagger2的ui界面:

    2、需要swagger2的这两个包:

            <!-- swagger2 包 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${springfox-swagger2-version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${springfox-swagger2-version}</version>
            </dependency>

    3、然后需要写配置swagger2的代码类:

    代码内容如下:

    package com.example.cloudorderdemo.config;
    
    import io.swagger.annotations.Api;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    @Configuration
    public class SwaggerConfig {

      //application.yml中配置的version @Value(
    "${info.app.version}") private String version;
    //application.yml中配置的应用名称 @Value(
    "${spring.application.name}") private String applicationName; @Bean public Docket createRestApi(){//如果在application.yml中配置了项目访问路径userdemo就要在swagger访问路径的端口后面加上 /userdemo/ //最新:swagger的访问路径:http://localhost:8880/swagger-ui.html# return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(true) .select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("order订单服务") .version(version) .build(); } }

    4、最后在application启动类上面加上注解:@EnableSwagger2

    5、controller类上面写上接口swagger的注释:

      类名上:

       

      方法名上:

    最后启动项目打开swagger文档地址:http://localhost:8882/swagger-ui.html#/

  • 相关阅读:
    magento删除local.xml后必须要页面安装
    magento后台无法打开
    ubuntu安装phpmyadmin
    数据结构有关于最优二叉树——最优哈夫曼树的建立过程和哈夫曼编码
    计算后缀表达式的过程(C#)
    mysql语句的相关操作整理
    mysql在控制台里出现中文问号问题
    Wampserver由橙变绿的解决过程
    重装win7系统的过程
    数据结构(C#):图的最短路径问题、(Dijkstra算法)
  • 原文地址:https://www.cnblogs.com/spll/p/16655263.html
Copyright © 2020-2023  润新知