【WEEK14】 【DAY4】Swagger Part 2【English Version】

2024.5.30 Thursday
Following up on 【WEEK14】 【DAY3】Swagger Part 1【English Version】

Contents

  • 16.4. Configure Scanned Interfaces
    • 16.4.1. Modify SwaggerConfig.java
      • 16.4.1.1. Use the .basePackage() Method to Specify the Package Path for Scanning
      • 16.4.1.2. Other Scanning Methods Can Be Found in the Source Code of RequestHandlerSelectors.class
    • 16.4.2. Continue to Modify SwaggerConfig.java
      • 16.4.2.1. Configure Interface Scanning Filters
      • 16.4.2.2. Other Methods:
  • 16.5. Configure Swagger Switch
    • 16.5.1. Modify the Value of enable to Disable Swagger
    • 16.5.2. Dynamically Configure Swagger to Display in Test and Dev Environments, but Not in Prod
      • 16.5.2.1. Modify SwaggerConfig
      • 16.5.2.2. Modify application.properties
      • 16.5.2.3. Create application-dev.properties
      • 16.5.2.4. Create application-pro.properties
      • 16.5.2.5. Restart

16.4. Configure Scanned Interfaces

Configuring scanned interfaces when building Docket using the select() method

16.4.1. Modify SwaggerConfig.java

16.4.1.1. Use the .basePackage() Method to Specify the Package Path for Scanning

package com.P47.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;import java.util.ArrayList;@Configuration  // Equivalent to @Component
@EnableSwagger2 // Enable Swagger2
public class SwaggerConfig {// Configure the bean instance Docket for Swagger, to set specific parameters for Swagger@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.class, select the appropriate version for editing.apiInfo(apiInfo()) // Public Docket(DocumentationType documentationType) method, click on ApiInfo to go to ApiInfo.class.select()   // Configure the scanning interface using the .select() method, RequestHandlerSelectors configures how to scan interfaces.apis(RequestHandlerSelectors.basePackage("com.P47.controll")).build();// Click into Docket.class to see the source code of various methods}// Configure Swagger information (apiInfo)private ApiInfo apiInfo(){// Prevent DEFAULT_CONTACT (name changed to contact) from reporting errorsContact contact = new Contact("Contact Name", "Contact URL", "Contact Email");return new ApiInfo("Swagger Api Documentation", // Title"Api Documentation Description", // Description"version 1.0",  // Version number"http://terms.service.url",  // Organization URLcontact,    // Contact information"Apache 2.0",   // License"http://www.apache.org/licenses/LICENSE-2.0",   // License URLnew ArrayList<>() // Extensions);}
}

After restarting, check: only the hello-controller section remains
Insert image description here

16.4.1.2. Other Scanning Methods Can Be Found in the Source Code of RequestHandlerSelectors.class

basePackage(final String basePackage) // Scan interfaces based on package path
any() // Scan all, all interfaces in the project will be scanned
none() // Do not scan interfaceswithMethodAnnotation(final Class<? extends Annotation> annotation)  // Scan based on method annotation, e.g., withMethodAnnotation(GetMapping.class) only scans GET requests
withClassAnnotation(final Class<? extends Annotation> annotation)   // Scan based on class annotation, e.g., withClassAnnotation(Controller.class) only scans interfaces in classes annotated with @Controller

16.4.2. Continue to Modify SwaggerConfig.java

16.4.2.1. Configure Interface Scanning Filters

Add the following filter

.paths(PathSelectors.ant("/P47/**"))    // Click on path to see the initialization of private Predicate<String> pathSelector; Here, using the .ant() method as an example, only scan interfaces with requests starting with /P47

After restarting, check: no methods are displayed
Insert image description here

16.4.2.2. Other Methods:

Insert image description here
Here is the translated content while keeping the non-comment parts of the code unchanged:

At this time, the complete modified code

package com.P47.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;import java.util.ArrayList;@Configuration  // Equivalent to @Component
@EnableSwagger2 // Enable Swagger2
public class SwaggerConfig {// Configure the bean instance Docket for Swagger, to set specific parameters for Swagger@Beanpublic Docket docket(){return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.class, select the appropriate version for editing.apiInfo(apiInfo()) // Public Docket(DocumentationType documentationType) method, click on ApiInfo to go to ApiInfo.class.select()   // Configure the scanning interface using the .select() method, RequestHandlerSelectors configures how to scan interfaces.apis(RequestHandlerSelectors.basePackage("com.P47.controll"))  // Use basePackage to specify the package to scan.paths(PathSelectors.ant("/P47/**"))    // Click on path to see the initialization of private Predicate<String> pathSelector; Here, using the .ant() method as an example, only scan interfaces with requests starting with /P47.build();// Click into Docket.class to see the source code of various methods/*RequestHandlerSelectors. MethodsbasePackage(final String basePackage) // Scan interfaces based on package pathany() // Scan all, all interfaces in the project will be scannednone() // Do not scan interfaceswithMethodAnnotation(final Class<? extends Annotation> annotation)  // Scan based on method annotation, e.g., withMethodAnnotation(GetMapping.class) only scans GET requestswithClassAnnotation(final Class<? extends Annotation> annotation)   // Scan based on class annotation, e.g., withClassAnnotation(Controller.class) only scans interfaces in classes annotated with @Controller*//*PathSelectors. Methodsany() // Scan any requestnone() // Do not scan any requestregex(final String pathRegex) // Control through regular expressionsant(final String antPattern) // Control through ant()*/}// Configure Swagger information (apiInfo)private ApiInfo apiInfo(){// Prevent DEFAULT_CONTACT (name changed to contact) from reporting errorsContact contact = new Contact("Contact Name", "Contact URL", "Contact Email");return new ApiInfo("Swagger Api Documentation", // Title"Api Documentation Description", // Description"version 1.0",  // Version number"http://terms.service.url",  // Organization URLcontact,    // Contact information"Apache 2.0",   // License"http://www.apache.org/licenses/LICENSE-2.0",   // License URLnew ArrayList<>() // Extensions);}
}

16.5. Configure Swagger Switch

Modify SwaggerConfig.java

16.5.1. Modify the Value of enable to Disable Swagger

@Bean
public Docket docket(){return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.class, select the appropriate version for editing.apiInfo(apiInfo()) // Public Docket(DocumentationType documentationType) method, click on ApiInfo to go to ApiInfo.class.enable(false)  // Whether to enable Swagger, if false it cannot be started, and the page shows: 😱 Could not render e, see the console..select()   // Configure the scanning interface using the .select() method, RequestHandlerSelectors configures how to scan interfaces//.apis(RequestHandlerSelectors.basePackage("com.P47.controll"))  // Use basePackage to specify the package to scan//.paths(PathSelectors.ant("/P47/**"))    // Click on path to see the initialization of private Predicate<String> pathSelector; Here, using the .ant() method as an example, only scan interfaces with requests starting with /P47.build();}

Restart:
Insert image description here

16.5.2. Dynamically Configure Swagger to Display in Test and Dev Environments, but Not in Prod

16.5.2.1. Modify SwaggerConfig

@Bean
public Docket docket(Environment environment){// Set the environments where Swagger should be displayedProfiles profiles = Profiles.of("dev", "test");// Determine if the current environment matches the profiles// Use enable() to accept this parameter and decide whether to display Swaggerboolean flag = environment.acceptsProfiles(profiles);return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.class, select the appropriate version for editing.apiInfo(apiInfo()) // Public Docket(DocumentationType documentationType) method, click on ApiInfo to go to ApiInfo.class.enable(flag)  // Whether to enable Swagger, if false it cannot be started, and the page shows: 😱 Could not render e, see the console..select()   // Configure the scanning interface using the .select() method, RequestHandlerSelectors configures how to scan interfaces//.apis(RequestHandlerSelectors.basePackage("com.P47.controll"))  // Use basePackage to specify the package to scan//.paths(PathSelectors.ant("/P47/**"))    // Click on path to see the initialization of private Predicate<String> pathSelector; Here, using the .ant() method as an example, only scan interfaces with requests starting with /P47.build();}

At this time, the complete code:

package com.P47.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;import java.util.ArrayList;@Configuration  // Equivalent to @Component
@EnableSwagger2 // Enable Swagger2
public class SwaggerConfig {// Configure the bean instance Docket for Swagger, to set specific parameters for Swagger@Beanpublic Docket docket(Environment environment){// Set the environments where Swagger should be displayedProfiles profiles = Profiles.of("dev", "test");// Determine if the current environment matches the profiles// Use enable() to accept this parameter and decide whether to display Swaggerboolean flag = environment.acceptsProfiles(profiles);return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.class, select the appropriate version for editing.apiInfo(apiInfo()) // Public Docket(DocumentationType documentationType) method, click on ApiInfo to go to ApiInfo.class.enable(flag)  // Whether to enable Swagger, if false it cannot be started, and the page shows: 😱 Could not render e, see the console..select()   // Configure the scanning interface using the .select() method, RequestHandlerSelectors configures how to scan interfaces//.apis(RequestHandlerSelectors.basePackage("com.P47.controll"))  // Use basePackage to specify the package to scan//.paths(PathSelectors.ant("/P47/**"))    // Click on path to see the initialization of private Predicate<String> pathSelector; Here, using the .ant() method as an example, only scan interfaces with requests starting with /P47.build();}// Configure Swagger information (apiInfo)private ApiInfo apiInfo(){// Prevent DEFAULT_CONTACT (name changed to contact) from reporting errorsContact contact = new Contact("Contact Name", "Contact URL", "Contact Email");return new ApiInfo("Swagger Api Documentation", // Title"Api Documentation Description", // Description"version 1.0",  // Version number"http://terms.service.url",  // Organization URLcontact,    // Contact information"Apache 2.0",   // License"http://www.apache.org/licenses/LICENSE-2.0",   // License URLnew ArrayList<>() // Extensions);}
}

16.5.2.2. Modify application.properties

spring.application.name=swagger-demo
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
spring.profiles.active=dev

16.5.2.3. Create application-dev.properties

# Production environment
server.port=8081

16.5.2.4. Create application-pro.properties

# Test environment
server.port=8082

16.5.2.5. Restart

Access http://localhost:8081/swagger-ui.html
Insert image description here
Change the configuration in application.properties to spring.profiles.active=pro, and access http://localhost:8082/swagger-ui.html to find that the Swagger page cannot be accessed.
Insert image description here
Similarly, if you do not specify a profile in spring.profiles.active, the default port is 8080. Due to the configuration in SwaggerConfig, http://localhost:8080/swagger-ui.html cannot access Swagger either.
Insert image description here

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/20325.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【案例实战】 基于OpenCV实现鹿茸面积计算

学习《人工智能应用软件开发》&#xff0c;学会所有OpenCV技能就这么简单&#xff01; 做真正的OpenCV开发者&#xff0c;从入门到入职&#xff0c;一步到位&#xff01; 有人在我得B站答疑群里发了下面的图&#xff1a; 问&#xff1a;如何计算鹿茸最外圈蜡皮面积占整个鹿茸…

基于STC12C5A60S2系列1T 8051单片机的TM1638键盘数码管模块的数码管显示与TM1638芯片连接的按键的按键值应用

基于STC12C5A60S2系列1T 8051单片机的TM1638键盘数码管模块的数码管显示与TM1638芯片连接的按键的按键值应用 STC12C5A60S2系列1T 8051单片机管脚图STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式及配置STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式介绍TM1638键盘…

word多级列表与上一级不匹配

问题&#xff1a;三级列表显示1.1.1&#xff0c;而实际情况应该是2.1.1&#xff0c;如何设置&#xff1f; 方法&#xff1a;鼠标在word原文停留在二级列表上&#xff0c;然后进入多级列表中设置三级列表

ChatGPT在工作中的使用案例

知识点提示 开发过程中&#xff0c;遇到某个知识点&#xff0c;忘记或者不清楚怎么使用了&#xff0c;通过ChatGPT快速生成使用提示和案例。代码库“字典” 比如C 11 判断数组所有元素为false 在 C11 中&#xff0c;可以使用标准库中的 all_of 算法来判断数组中的所有元素是…

根据TVbox修改而来的盒子双播软件-电视盒子内置源版-点播+直播双播盒子软件-供大家学习研究参考

是一款根据TVbox修改而来的盒子双播软件,经过无数次的更改适配了手机、平板、智能电视和TV盒子,并且支持安卓4.x,所以对很多旧的智能电视和盒子支持是相当友好的,也越来越好用,且对软件部分做出修改优化。内置超多优质资源,汇集了众多影视媒体资源,画质也非常的感人,并…

p2p文件传输小工具

使用webRTC的相关技术栈可以很轻松的开发一个p2p文件传输工具&#xff0c;这里主要讲下使用datachannel开发的一个文件传输工具client程序的使用 客户端A&#xff1a;需要可以访问公网&#xff0c;运行client的主机 客户端B&#xff1a;可以访问公网&#xff0c;可以和客户端…

云数融合与大数据技术在日常生活中的创新应用探索

前言 移动云模型服务产品在中国移动旗下主要包括云计算、大数据、人工智能等服务&#xff0c;它依托广泛的算力资源(4N31X)、丰富的网络接入资源和高品质云专网&#xff0c;实现算网端资源一站式开通&#xff0c;构建企业级一体化解决方案。 文章目录 前言云计算的日常应用智…

C++入门3——类与对象2(类的6个默认成员函数)

目录 1.类的6个默认成员函数 2. 构造函数 2.1 构造函数的概念 2.2 构造函数的特性 3. 析构函数 3.1 析构函数的概念 3.2 析构函数的特性 4.拷贝构造函数 4.1 拷贝构造函数的概念 4.2 拷贝构造函数的特性 5.赋值运算符重载函数 5.1运算符重载函数 5.2 赋值运算符重…

每日一题——Python实现PAT乙级1020 月饼(举一反三+思想解读+逐步优化)

一个认为一切根源都是“自己不够强”的INTJ 个人主页&#xff1a;用哲学编程-CSDN博客专栏&#xff1a;每日一题——举一反三Python编程学习Python内置函数 Python-3.12.0文档解读 目录 我的写法 专业点评&#xff1a; 时间复杂度分析&#xff1a; 空间复杂度分析&#…

【一步一步了解Java系列】:子类继承以及代码块的初始化

看到这句话的时候证明&#xff1a;此刻你我都在努力 加油陌生人 个人主页&#xff1a;Gu Gu Study专栏&#xff1a;一步一步了解Java 喜欢的一句话&#xff1a; 常常会回顾努力的自己&#xff0c;所以要为自己的努力留下足迹 喜欢的话可以点个赞谢谢了。 作者&#xff1a;小闭 …

分享一个在linux中运行通义千问的方法

分享一个在linux中和通义千问交互的方法 效果展示: 整体步骤 分享一个在linux中和通义千问交互的方法效果展示:一、在阿里云appflow控制台创建连接流1、通过以下地址,在灵积平台创建个API-KEY,用于通义千问的连接凭证2、点击连接流-创建连接流3、第一步选择webhook4.第二步…

nginx和proxy_protocol协议

目录 1. 引言2. HTTP server的配置3. Stream server的配置3.1 作为proxy_protocol的前端服务器3.2 作为proxy_protocol的后端服务器1. 引言 proxy_protocol 是haproxy开发的一种用于在代理服务器和后端服务器之间传递客户端连接信息的协议。使用 proxy_protocol 的主要优势是能…

【ai】livekit服务本地开发模式1:example app信令交互详细流程

文档要安装git lfs 下载当前最新版本1.6.1windows版本:启动dev模式 服务器启动 (.venv) PS D:\XTRANS\pythonProject\LIVEKIT> cd .

VS2022+QT5.15.2+MySQL8.4大集合

网上的教程都建议用Qt5&#xff0c;不要用6&#xff0c;不死心的尝试了整整一天失败了&#xff0c;乖乖用回5&#xff0c;qt5需要编译一下生成mysql的动态和静态库 1. mysql8.4安装 下载社区开发版&#xff0c;注意要64位 https://dev.mysql.com/downloads/mysql/ 配置一下数…

安卓SystemServer进程详解

目录 一、概述二、源码分析2.1 SystemServer fork流程分析2.1.1 [ZygoteInit.java] main()2.1.2 [ZygoteInit.java] forkSystemServer()2.1.3 [Zygote.java] forkSystemServer()2.1.4 [com_android_internal_os_Zygote.cpp]2.1.5 [com_android_internal_os_Zygote.cpp] ForkCom…

CANDela studio基础使用

ECU Information 可以修改ECU的名称 里面有个Supported Interfaces&#xff0c;可以在CDDT里面选择支持的通讯接口 可以在tools下面新建internface&#xff0c;也可以从其他CDDT文件里面复制过来&#xff0c;复制的时候注意要另外将里面的参数再复制一次。 也可以在这里点击新…

倪师哲学。能让我好,我就接受

还有有些人更搞笑的是&#xff0c;把自己的行为啊&#xff0c;建立在别人的基础之上&#xff0c;如果那个人么样对我&#xff0c;我肯定能怎么样对这个人。 生而为人呐&#xff0c;你是一个独立的人&#xff0c;不要去总是拿着各种各样的前提&#xff0c;来限制了自己个人的成长…

打造你的专属Vue组件:超实用“手机号、邮箱、身份证号加密显示组件“实战

随着Web应用程序的发展&#xff0c;我们经常需要处理用户敏感信息&#xff0c;如手机号码和身份证号码。为了保护用户隐私&#xff0c;我们需要在前端对这些信息进行加密处理&#xff0c;以避免直接暴露在页面上。在这篇博客中&#xff0c;我们将介绍如何使用Vue 3.0单文件组件…

CATO原理中的数学与魔术(十一)——Parity Principle及其应用二:集合的可视化...

早点关注我&#xff0c;精彩不错过&#xff01; 上篇文章中&#xff0c;我们已经进入了CATO原理魔术介绍的深水区&#xff0c;是第3个系列Parity Principle中集合性质的章节&#xff0c;聊到了关于张数和求和集合性质&#xff0c;并对性质之间的偏序关系&#xff0c;性质之间的…

three.js官方案例(animation / keyframes)webgl_animation_keyframes.html学习

目录 ​编辑 1 PMREMenerator 1.1 构造函数 1.2 fromScene方法 2 AnimationMixer 3 animal1.html全部 4 animal1.js全部 1 PMREMenerator 此类生成预过滤的 Mipmapped 辐射环境贴图 &#xff08;PMREM&#xff09; 来自 cubeMap 环境纹理。这允许不同的级别 的模糊&…