温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在SpringBoot中应用Swagger

发布时间:2021-05-08 15:09:59 来源:亿速云 阅读:109 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关如何在SpringBoot中应用Swagger,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

一、基本使用

使用时需要导入下面的依赖:

<!--引入swagger,自动生成api说明文档-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

然后书写下面的配置类即可完成配置!

@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
}

浏览器访问http://localhost:8910/swagger-ui.html,即可可以看到下面的界面:

如何在SpringBoot中应用Swagger

二、自定义描述内容

@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启动swagger
//                .enable(false)
                .groupName("卢泽龙")
                .select()
                //RequestHandlerSelectors:配置要扫描的接口方式
//                    basePackage : 指定要扫描的包
                    .apis(RequestHandlerSelectors.basePackage("org.lzl.laboratory.controller"))
//                    any:扫描全部
//                    .apis(RequestHandlerSelectors.any())
//                    none:不扫描
//                    .apis(RequestHandlerSelectors.none())
//                    withClassAnnotation:扫描类上的注解
//                    .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
//                    withMethodAnnotation:扫描方法上的注解
//                    .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                //path() 过滤什么路径
//                    .paths(PathSelectors.ant("/lzl/**"))
                .build();
    }

 //配置swagger信息 ==> apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("卢泽龙","https://blog.csdn.net/MoastAll","382491212@qq.com");

        return new ApiInfo(
                "云上实验室的API文档说明",
                "abcdefg",
                "v1.0",
                "https://blog.csdn.net/MoastAll",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
 }

其中如果有这样的需求:在dev,test环境中开启swagger,其他环境不开启swagger,我们只需要给Docket加上下面的environment参数,再用acceptsProfiles方法来判断环境是否符合要求!

如何在SpringBoot中应用Swagger

使用的效果为:

如何在SpringBoot中应用Swagger

三、给下面的models和接口一些提示信息

1.给实体类加入下面的注解

如何在SpringBoot中应用Swagger

http://localhost:8910/swagger-ui.html的models就会呈现下面的界面:

如何在SpringBoot中应用Swagger

2.给controller加上下面的注解

如何在SpringBoot中应用Swagger

就会出现下面的提示信息:

如何在SpringBoot中应用Swagger

以上就是如何在SpringBoot中应用Swagger,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI