SpringBoot使用Swageer文档编辑框架 (狂神)

Swageer 协调前后端,可测试后端接口,最流行的API框架,RestFul 风格Api文档在线自动生成工具 

swagger 简单使用  

1、导入依赖

<!--        swagger依赖-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

2.创建Hello工程

3、配置swagger2  

@Configuration
@EnableSwagger2  //开启swagger
public class SwaggerConfiger {
    
}

运行测试,可通过http://localhost:8080/swagger-ui.html访问

SpringBoot无法访问swagger-ui.html(404)

因为资源路径映射问题,SpringBoot无法找到swagger-ui.html可以添加配置解决

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    /**
     * 访问静态资源
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * SpringBoot自动配置本身并不会把/swagger-ui.html
         * 这个路径映射到对应的目录META-INF/resources/下面
         * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
         */
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
        super.addResourceHandlers(registry);
    }

}

如果还是不行,检查是否依赖版本过高,测试3.0.0在添加上面的映射路径后还是无效,换低版本后成功。

参考

https://blog.csdn.net/lovequanquqn/article/details/90705721

https://www.cnblogs.com/yuanweidao/p/13710194.html

自定义配置Swagger

@Configuration
@EnableSwagger2  //开启swagger
public class SwaggerConfiger {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                分组名字
                .groupName("看向未来213")
                //设置扫描路径,默认全扫描
                .select()
                // any 扫描全部
                //none 不扫描
                //basePackage 扫描包
                //withMethodAnnotation  按特定方法上注解类扫描
                //withClassAnnotation   按特定类注解扫描
.apis(RequestHandlerSelectors.basePackage("com.example.springbootswageer.controller"))
//                .paths(PathSelectors.ant("com/**"))    //过滤
                .build();
    }

    public ApiInfo apiInfo(){
        Contact contact = new Contact("看向未来213", "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343", "2621302586qq.com");
        return new ApiInfo(
                "Api Documentation",
                "Swagger初识",
                "v1.0",
                "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList());
    }
}

配置APi文档的分组

  .groupName("看向未来213")

给Api文档添加注释的五个注解

@Api("用户实体类") 
@ApiModel("用户实体类")
两个一样是标注实体类的注解


@ApiModelProperty("用户名")  给属性添加注解

@ApiOperation("方法注释")  给方法添加注解

@ApiParam("用户名") 给参数加注解

try it out 可以在网站上测试,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值