美文网首页Spring Boot
swagger简介及使用

swagger简介及使用

作者: 飞逝1 | 来源:发表于2018-09-27 16:52 被阅读0次

    swagger的由来

    • 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。
    • 前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让你更好的书写API文档的框架。

    同类技术

    swagger生态图

    20170827202033991.png

    swagger UI

    示例


    整个页面.png

    展开界面


    展开界面.png

    swagger-editor

    就是一个在线编辑文档说明文件(swagger.json或swagger.yaml文件)的工具,以方便生态中的其他小工具(swagger-ui)等使用。 左边编辑,右边立马就显示出编辑内容来。


    swagger-editor.jpg

    使用方法

    • 在pom中引入依赖

      <dependency>
        <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
        <version>1.8.0.RELEASE</version>
      </dependency>
      
    • 在应用主类中增加@EnableSwagger2Doc注解

    @SpringBootApplication
    @EnableSwagger2Doc
        public class Springboot2Swagger2Application {
            public static void main(String[] args) {
            SpringApplication.run(Springboot2Swagger2Application.class, args);
        }
    }
    

    默认情况下就能产生所有当前SpringMVC加载的请求映射文档
    访问地址:http://localhost:8080/swagger-ui.html

    • 参数配置示例

      swagger.enabled=true
      swagger.title=spring-data-jpa module
      swagger.description=Starter for swagger 2.x
      swagger.license=Apache License, Version 2.0
      swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
      swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-    swagger
      swagger.contact.name=RanRan Tao
      swagger.contact.url=https://www.jianshu.com/u/822585e5c69a
      swagger.contact.email=16422802@qq.com
      swagger.base-package=com.example.springdatajpa.controller
      swagger.base-path=/**
      swagger.exclude-path=/error, /ops/**
      

    代码

    https://github.com/1094064939/SpringBootExample

    相关文章

      网友评论

        本文标题:swagger简介及使用

        本文链接:https://www.haomeiwen.com/subject/spneoftx.html