swagger常用注解

@ApiModel("") -----------------写在实体类模块的上方,对实体类进行说明
@Api(tags = “xxxx”)----------写在Controller模块的上方,对Controller模块进行说明
@ApiParam("")-----------------写在传入参数的前方,对参数进行说明
@ApiOperation(value = “”)------------写在Controller模块内的方法上面,对方法进行说明

swagger的使用

1、导入依赖
注意如果要使用新版本的swagger3.0.0,导包被简化成一个,而不像之前要导两个

<!--开启swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

以下是swagger2,x版本的依赖包。swagger2和swagger3有在使用上有一些的区别,下面我都是按照新版本的swagger3来测试

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

2、需要写一个swagger的配置类
// 只有扫描controller目录才不会出现basis error

@Configuration
// 不需要添加Enable注解了
public class SwaggerConfig {

    // 配置Docket的bean实例
    @Bean
    public Docket docket() {
        // 调用apiInfo
        return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        /*select-api-paths是固定搭配*/
                .select()
                /**
                 * apis():指定扫描的接口
                 *  RequestHandlerSelectors:配置要扫描接口的方式
                 *       basePackage:指定要扫描的包
                 *       any:扫面全部
                 *       none:不扫描
                 *       withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
                 *       withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
                 */
                 // 根据目录名进行修改
                 // 只有扫描controller目录才不会出现basis error
                .apis(RequestHandlerSelectors.basePackage("com.controller"))
                /**
                 * paths():过滤路径
                 *  PathSelectors:配置过滤的路径
                 *      any:过滤全部路径
                 *      none:不过滤路径
                 *      ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
                 *      regex:过滤指定路径:按照String的matches方法进行匹配
                 */
                .paths(PathSelectors.any())
                .build();;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx") // 标题
                .description("本文档描述了xxx后端接口文档") // 描述
                .version("1.0") // 版本
                .build(); // 创建
    }
}

3、访问测试 :http://localhost:9090/swagger-ui/index.html [注意是/index.html而不是原来的直接html] ,可以看到swagger的界面。这个端口号要根据自己的后端端口号进行修改。这样就可以进入swagger的界面了。
在这里插入图片描述
感觉这个原生的swagger不太好看。可以导入github上面别人写的依赖包【有最新的包但是我一直用不了不知道为什么,哭了~,只用能老版本的包,19年就停止更新了】

		<dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

推荐大家去使用新版本的包,新版本前端比旧版本好看多

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

然后去访问地址:http://localhost:9090/doc.html。就可以看到不一样的界面了

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐