栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合swagger

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • Swagger是什么
  • 一、引入Swagger依赖
  • 二、使用Swagger
    • 1.引入库


Swagger是什么

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。


一、引入Swagger依赖

我这里的版本是2.9的,如果导入出错了可以更换springboot版本,或者换其他版本的swagger
https://mvnrepository.com/artifact/io.springfox/ 可以到maven仓库中选择

 
        
            io.springfox
            springfox-swagger2
            2.9.2
        

        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
二、使用Swagger 1.引入库

代码如下(示例):

package com.example.demo.config;

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.builders.RequestHandlerSelectors;
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 SwaggerConfig {
    
    @Bean
    public Docket restApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("标准接口")
                .apiInfo(apiInfo("Spring Boot中使用Swagger2构建RESTful APIs", "1.0"))
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    
    private ApiInfo apiInfo(String title, String version) {
        return new ApiInfoBuilder()
                .title(title)
                .description("更多请关注: https://blog.csdn.net/GGBOY__")
                .termsOfServiceUrl("https://blog.csdn.net/GGBOY__")
                .contact(new Contact("GGBOY__", "https://blog.csdn.net/GGBOY__", "2925090754@qq.com"))
                .version(version)
                .build();
    }
}

.basePackage中添加接口所在的包,这样才可以测试写的接口。
启动springboot程序后,访问http://localhost:9090/swagger-ui.html,记得改成自己对应的端口号。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号