• idea集成swagger创建接口文档


    idea集成swagger创建接口文档

    1.1 pom.xml引入依赖

    <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
                <version>1.5.22</version>
                <exclusions>
                    <exclusion>
                        <artifactId>swagger-annotations</artifactId>
                        <groupId>io.swagger</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    1.2 config文件夹下创建SwaggerConfig

    package com.dtdream.dtsphere.bgsh.reportReview.config.Swagger;
    
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    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;
    
    
    /**
     * @description: swagger配置
     * @author: z0803
     * @create: 2020/06/28
     **/
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi() {
    
            return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false).apiInfo(apiInfo()).select()
                    .apis(RequestHandlerSelectors.basePackage("com.dtdream.dtsphere.bgsh.reportReview"))
                    // .paths(PathSelectors.any())
                    .build();
        }
    
        @Bean
        public ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("生态多项目申请授权项目手册").description("获取主空间、门户表清单和申请表清单的所有API接口").version("V1.0")
                    .build();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    1.3 接口信息引入

    
    @Api(tags = "主空间、门户表清单类接口")
    @RestController
    @Slf4j
    @RequestMapping("/audit")
    public class AuditController {
        @Autowired
        private IAuditService auditService;
    
        /**
         * 查询待审核清单列表
         * @param page 页数
         * @param pageSize 查询个数
         * @param name 任务编号
         * @param industry 行业
         * @param catalogType 清单类型
         * @return 待审核清单列表
         */
        @ApiOperation(value = "查询申请表清单列表", notes = "查询申请表清单列表")
        @GetMapping("/unauditList")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page", value = "页数", dataType = "int", paramType = "query", required = true),
                @ApiImplicitParam(name = "pageSize", value = "查询个数",
                        dataType = "int", paramType = "query", required = true),
                @ApiImplicitParam(name = "name", value = "未审核名称", dataType = "String", paramType = "query"),
                @ApiImplicitParam(name = "industry", value = "行业", dataType = "int", paramType = "query"),
                @ApiImplicitParam(name = "catalogType", value = "清单类型",dataType = "String", paramType = "query")})
        @PermissionCheck
        public Response getUnauditList(@RequestParam("page") Integer page,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(value = "name",required = false) String name,
                                       @RequestParam(value = "industry",required = false) Long industry,
                                       @RequestParam(value = "catalogType",required = false) String catalogType) {
    
            return auditService.getUnauditList(page, pageSize, name, industry, catalogType);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    1.4 运行项目打开swagger

    http://127.0.0.1:端口/接口前缀/swagger-ui.html#/
    结果如图所示即为成功
    在这里插入图片描述

    1.5 常用Swagger注解

    在Java类中添加Swagger的注解即可生成Swagger接口,常用Swagger注解如下:
    @Api:修饰整个类,描述Controller的作用
    @ApiOperation:描述一个类的一个方法,或者说一个接口
    @ApiParam:单个参数描述
    @ApiModel:用对象来接收参数
    @ApiModelProperty:用对象接收参数时,描述对 象的一个字段
    @ApiResponse:HTTP响应其中1个描述
    @ApiResponses:HTTP响应整体描述
    @ApiIgnore:使用 该注解忽略这个API
    @ApiError :发生错误返回的信息
    @ApiImplicitParam:一个请求参数
    @ApiImplicitParams:多个请求参数

    1.6 编写swagger报错

    供参考,跟我一样报错信息的可借鉴

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2022-06-27 11:32:25,409 ERROR   5447 --- [           main] o.springframework.boot.SpringApplication : Application run failed
    org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
    	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)
    	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54)
    	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
    	at java.lang.Iterable.forEach(Iterable.java:75)
    	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155)
    	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123)
    	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586)
    	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
    	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    	at com.dtdream.dtsphere.bgsh.reportReview.ReportReviewApplication.main(ReportReviewApplication.java:15)
    Caused by: java.lang.NullPointerException: null
    	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112)
    	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109)
    	at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37)
    	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
    	at java.util.TimSort.sort(TimSort.java:220)
    	at java.util.Arrays.sort(Arrays.java:1438)
    	at com.google.common.collect.Ordering.sortedCopy(Ordering.java:842)
    	at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57)
    	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138)
    	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135)
    	at com.google.common.collect.Iterators$6.transform(Iterators.java:783)
    	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
    	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47)
    	at com.google.common.collect.Iterators$ConcatenatedIterator.hasNext(Iterators.java:1333)
    	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:268)
    	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:232)
    	at com.google.common.collect.FluentIterable.toList(FluentIterable.java:619)
    	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111)
    	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96)
    	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167)
    	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
    	... 12 common frames omitted
    
    Process finished with exit code 1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在application.yml 加上下面代码即可

    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    
    • 1
  • 相关阅读:
    web server apache tomcat11-09-JNDI Datasource
    自定义MVC增删改查
    看图就懂|衡量业务增长健康的销售指标如何选择
    猿创征文|Jedis
    Ansible概述和模块解释
    Django设置中文
    Vue3 中如何加载动态菜单?
    Android SurfaceFlinger导读(02)MessageQueue
    Lambda表达式
    数据结构与算法:配对堆
  • 原文地址:https://blog.csdn.net/weixin_44768794/article/details/125480211