10、Swagger配置

学习目标:

  • 了解Swagger的作用和概念
  • 了解前后端分离
  • 在spring boot中集成Swagger

Swagger简介:

  • 前后端分离 
  1. vue+springboot
  2. 后端时代:前端只用管理静态页面;html ==>后端。模板引擎:jsp==>后端是主力
  • 前后端分离时代:
  1. 后端:后端控制层、服务层、数据访问层【后端团队】
  2. 前端:前端控制层、视图层【前端团队】     

                    伪造后端数据,json。已经存在了、不需要后端、前端工程依旧能跑起来。

  1. 前后端如何交互===》API
  2. 前后端相对独立、松耦合
  3. 前后端甚至可以部署在不同的服务器上

产生一个问题:

  • 前后端继承联调、前端人员和后端人员无法左到“即使协商、尽早解决”、最终导致问题集中爆发

解决方案:

  • 首先指定schema[计划的提纲]、实时更新最新的API、降低集成风险
  • 早些年:制定word计划文档;
  • 前后端分离:

               前端测试后端接口:postman

                后端提供接口、需要实时更新最新的消息及改动

 

Swagger

号称世界上最流行的API框架

RestFul API文档在线自动生成工具====》API文档与API定义同步更新

直接运行、可以在线测试API接口

支持多种语言

 

 

在项目中使用Swagger需要springbox

  • swagger2
  • ui

springboot集成gengSwagger

  1. 新建一个springboot-web项目
  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.1</version>
</dependency>

      Springfox Swagger2

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

         3.编写一个Hello工程

         4.配置swagger

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

       这两个依赖的版本用最新的会得不到请求,

 

 

 

 

 

 

           5.测试运行:http://localhost:8080/swagger-ui.html

         

配置swagger

  • swagger的bean实例Docket;

SwaggerConfig:

package com.dudu.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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;

import java.util.ArrayList;

/**
 * @Author 嘟嘟嘟
 * @ClassName SwaggerConfig
 * @Project swagger-demo
 * @CreateTime 2021/1/6 17:38
 */
@Configuration
@EnableSwagger2//开启Swagger2
public class SwaggerConfig {
    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    //配置swagger信息 apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact=new Contact("嘟嘟","http://localhost:8080/swagger-ui.html","944969864@qq.com");
        return new ApiInfo(
                "嘟嘟的swaggerAPI文档",
                "我是啦啦啦",
                "1.0",
                "http://localhost:8080/swagger-ui.html",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}
HelloController:
package com.dudu.swagger.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 嘟嘟嘟
 * @ClassName HelloController
 * @Project swagger-demo
 * @CreateTime 2021/1/6 17:32
 */
@RestController
public class HelloController {
    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }
}

 

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dudu</groupId>
    <artifactId>swagger-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>swagger-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>


        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Swagger配置扫描接口

Docket().select

配置是否启动swagger

   //配置swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //获取项目的环境  通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//enable是否启动swagger,如果为false,则swagger不能在浏览器访问
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                // basePackage指定要扫描的包
                // any() 扫描全部
                // none() 不扫描
                //withClassAnnotation 扫描类上的注解、参数是一个注解的反射对象
                //withMethodAnnotation 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.dudu.swagger.controller"))
                //paths() 过滤什么路径
               //  .paths(PathSelectors.ant("/dudu/**"))
                .build();
    }

在swagger生产环境中使用,在发布时候不使用

  • 判断是不是生产环境    flag=false
  • 注入enable(flag)
public class SwaggerConfig {
    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的swagger环境
        Profiles profiles=Profiles.of("dev","test");
        //获取项目的环境  通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//enable是否启动swagger,如果为false,则swagger不能在浏览器访问
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                // basePackage指定要扫描的包
                // any() 扫描全部
                // none() 不扫描
                //withClassAnnotation 扫描类上的注解、参数是一个注解的反射对象
                //withMethodAnnotation 扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.dudu.swagger.controller"))
                //paths() 过滤什么路径
               //  .paths(PathSelectors.ant("/dudu/**"))
                .build();
    }

 

   

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值