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

2024-06-01 14:52

本文主要是介绍【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

这篇关于【WEEK14】 【DAY4】Swagger Part 2【English Version】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021310

相关文章

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

springboot 整合swagger

没有多余废话,就是干 spring-boot 2.7.8 springfox-boot-starter 3.0.0 结构 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/

Level3 — PART 3 — 自然语言处理与文本分析

目录 自然语言处理概要 分词与词性标注 N-Gram 分词 分词及词性标注的难点 法则式分词法 全切分 FMM和BMM Bi-direction MM 优缺点 统计式分词法 N-Gram概率模型 HMM概率模型 词性标注(Part-of-Speech Tagging) HMM 文本挖掘概要 信息检索(Information Retrieval) 全文扫描 关键词

MySQL record 02 part

查看已建数据库的基本信息: show CREATE DATABASE mydb; 注意,是DATABASE 不是 DATABASEs, 命令成功执行后,回显的信息有: CREATE DATABASE mydb /*!40100 DEFAULT CHARACTER SET utf8mb3 / /!80016 DEFAULT ENCRYPTION=‘N’ / CREATE DATABASE myd

Jenkins 通过 Version Number Plugin 自动生成和管理构建的版本号

步骤 1:安装 Version Number Plugin 登录 Jenkins 的管理界面。进入 “Manage Jenkins” -> “Manage Plugins”。在 “Available” 选项卡中搜索 “Version Number Plugin”。选中并安装插件,完成后可能需要重启 Jenkins。 步骤 2:配置版本号生成 打开项目配置页面。在下方找到 “Build Env

Learn ComputeShader 09 Night version lenses

这次将要制作一个类似夜视仪的效果 第一步就是要降低图像的分辨率, 这只需要将id.xy除上一个数字然后再乘上这个数字 可以根据下图理解,很明显通过这个操作在多个像素显示了相同的颜色,并且很多像素颜色被丢失了,自然就会有降低分辨率的效果 效果: 但是这样图像太锐利了,我们加入噪声去解决这个问题 [numthreads(8, 8, 1)]void CSMain(uint3 id

Vue3图片上传报错:Required part ‘file‘ is not present.

错误 "Required part 'file' is not present" 通常表明服务器期望在接收到的 multipart/form-data 请求中找到一个名为 file 的部分(即文件字段),但实际上没有找到。这可能是因为以下几个原因: 请求体构建不正确:在发送请求时,可能没有正确地将文件添加到 FormData 对象中,或者使用了错误的字段名。 前端代码错误:在前端代码中,可能

Unsupported major.minor version 52.0 错误解决方法

自己前些天的项目突然出现这个问题,经过仔细排查,发现有两个原因都会导致这个问题。 第一个就是POM文件中的dependency重复,如果使用的是maven导入,重复写入dependency就会出现该错误。 第二个是版本不匹配,即所引用的jar包太新,并不匹配你的jdk,因为我们正常用的都是jdk7,但是现在已经更新到jdk10了,好多最新版本最新版本的jar包都是基于最新的jdk编写,所以可以

【Swagger】SpringBoot整合Swagger

一、搭建Swagger组件 1. 引入Swagger 快速在SpringBoot(版本:2.6.3)项目中引入Swagger相应pom.xml: <dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></depende

C++入门(part 2)

前言 在前文我们讲解了C++的诞生与历史,顺便讲解一些C++的小语法,本文会继续讲解C++的基础语法知识。 1. 缺省参数 1.1缺省参数的概念 缺省参数是声明或定义函数时为函数的参数指定⼀个缺省值。在调⽤该函数时,如果没有指定实参则采⽤该形参的缺省值,否则使用指定的实参。(有些地⽅把缺省参数也叫默认参数) 1.2 缺省参数的分类 缺省参数分为全缺省和半缺省参数,全缺省就是全部形参给