Swagger学习及运用(一)-了解Swagger的概念及与项目集成介绍

了解Swagger的概念及与项目集成介绍

 Swagger

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

    特点:

  • Reatful API文档在线自动生成器  --》API文档与API定义同步更新
  • 可在线测试API
  • 支持多种语言(Java、PHP等)
  • 官网:https://swagger.io/

项目中集成Swagger

  • 项目环境

            (1)JDK1.8及其以上(注意必须是1.8及以上版本版本)

            (2)Spring 5.1.6

            (3)Mybatis 3.5.1

  • Spring MVC集成springfox-swagger2构建Restful API

            Maven依赖

              (1) springfox-swagger2

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


              (2)springfox-swagger-ui

<!-- 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>


              (3)guava

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>20.0</version>
</dependency>


              (4)mapstruct-jdk8

<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.2.0.Final</version>
</dependency>


              (5) Jackson
                      -- jackson-core
                      -- jackson-databind
                      -- jackson-annotations

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
</dependency>

 

  • 集成配置步骤

            (1) 在pom.xml文件中添加Swagger相关依赖

            (2) Swagger2配置类SwaggerConfig.java

                       注解说明:

                            @ComponentScan:设置Swagger扫描包

                            @EnableSwagger2: 使Swagger2生效

                            @Configuration:自劢在本类上下文加载一些环境变量信息

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //声明该类为配置类
@EnableSwagger2 //声明启动Swagger2
@EnableWebMvc //声明启动mvc
public class SwaggerConfig {
    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("dcc"))//扫描的包路径
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("DCC API接口")//文档说明
                .version("1.0.0")//文档版本说明
                .build();
    }
}

            (3)Spring MVC配置文件

                        --  添加<mvc:default-servlet-handler /> 设置springMVC的静态文件使用默认标记,防止部分js、css静态文件读取不到导致报404错误

                        -- 添加指定扫描:<context:component-scan/>

 

  • 具体运用(后续章节详细介绍)

        通过在API上添加注解实现,API文档的同步效果
        @Api
            表明可供Swagger展示的接口类(用在类上面)
        @ApiOperation
            描述API方法(用在方法上面)
        @ApiParam
            单个参数描述

        @ApiModel
            用对象接收参数(用在类上面)
        @ApiModelProperty
            用对象接收参数时,描述对象的一个字段(用在属性上面)
       

  • 与Nginx配置注意

        (1)访问Swagger界面

              http://IP:port/{context-path}/swagger-ui.html

        (2)问题

                生产环境下,只开放80端口,通过Tomcat无法访问Swagger

        (3)解决方案

              通过Nginx进行Swagger的访问
                  nginx.conf
                  http://IP/{context-path}/swagger-ui.html

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值