其他分享
首页 > 其他分享> > 公共模块配置swagger测试

公共模块配置swagger测试

作者:互联网

在项目中添加依赖

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

在公共模块中创建子模块service_utils

添加测试类

package com.lzq.yygh.common.config;

import com.google.common.base.Predicates;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                //.paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build();
    }
    @Bean
    public Docket adminApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }
    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("lzq", "http://123.com", "1938760862@qq.com"))
                        .build();
    }
    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了后台管理系统微服务接口定义")
                .version("1.0")
                .contact(new Contact("lzqz", "http://123.com","1938760862@qq.com"))
                        .build();
    }
}

因为测试service所以在service添加依赖

<dependency>
    <groupId>com.lzq</groupId>
    <artifactId>service_utils</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

因为启动类在service,所以common模块中配置注解也不会随类启动而加载

在启动类加入注解

@ComponentScan(basePackages = {"com.lzq"})

因为依赖引入所以会加载

测试

http://localhost:8201/swagger-ui.html

添加中文注释

在控制层上

@Api(tags = "医院设置接口")

方法上

@ApiOperation(value = "医院设置列表)

参数上

public boolean removeById(@ApiParam(name = "id", value = "医院id", required = true)@PathVariable String id)

标签:swagger,模块,service,documentation,测试,new,import,springfox,com
来源: https://blog.csdn.net/weixin_52210557/article/details/122566211