【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

相关文章

Java compiler level does not match the version of the installed Java project facet. map解决方法

右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面。 在页面中的“Java”下拉列表中,选择相应版本就OK了。

由于jdk版本问题引起的Unsupported major.minor version 52.0

tomcat启动报错 错误日志信息: 三月 01, 2019 9:47:02 下午 org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring root WebApplicationContext 三月 01, 2019 9:47:08 下午 org.apache.catalina.core.Standard

【漏洞复现】AJ-Report开源数据大屏 verification;swagger-ui RCE漏洞

0x01 产品简介 AJ-Report是一个完全开源的B平台,酷炫大屏展示,能随时随地掌控业务动态,让每个决策都有数据支撑。多数据源支持,内置mysql、elasticsearch、kudu等多种驱动,支持自定义数据集省去数据接口开发,支持17+种大屏组件,不会开发,照着设计稿也可以制作大屏。三步轻松完成大屏设计:配置数据源–>写SQL配置数据集->拖拽配置大屏->保存发布。欢迎体验。 0x0

urfread刷算法题day4|27. 移除元素+复习

27. 移除元素 题目描述 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。假设 nums 中不等于 val 的元素数量为 k,要通过此题,您需要执行以下操作:更改 nums 数组,使 nums 的前 k 个元素包含不等于 val 的元素。nums 的其余元素和 nums

同时使用接口文档swagger和knife4j

项目场景: springboot项目中同时使用接口文档swagger和knife4j 问题描述 在实体类中设置了字段必填的属性,在访问接口文档时出现异常 实体类关键代码片段 /*** 部门表 sys_dept*/public class SysDept extends BaseEntity{private static final long serialVersionUID =

SpringBoot【3】集成 Swagger

SpringBoot 集成 Swagger 前言pom.xml 配置文件application.yml 配置文件config 包Swagger2Config entity 包UserEntity service 包impl 包SwaggerServiceImpl SwaggerService controller 包SwaggerController SwaggerApplication验证

【进阶篇-Day4:使用JAVA编写石头迷阵游戏】

目录 1、绘制界面2、打乱石头方块3、移动业务4、游戏判定胜利5、统计步数6、重新游戏7、完整代码: 1、绘制界面 上述思路是:使用一个二维数组存放图片的编号,然后在后持遍历即可获取对应的图片。 代码如下: package com.itheima.stonepuzzle;import javax.swing.*;public cla

Windows 11 version 23H2 中文版、英文版 (x64、ARM64) 下载 (updated Jun 2024)

Windows 11 version 23H2 中文版、英文版 (x64、ARM64) 下载 (updated Jun 2024) Windows 11, version 23H2,企业版 arm64 x64 请访问原文链接:https://sysin.org/blog/windows-11/,查看最新版。原创作品,转载请保留出处。 作者主页:sysin.org Windows 11

System.Runtime, Version=6.0.0.0,生成的dll使用出现错误问题

解决: 1.unity左上角file点击选中build settings 点击player settings ,然后在player的window的other settings的configuration更改为 Framerwork 其实这个不换也可以的,我后面调试完,发现这个不是重点,下面第2点才是重点 2.然后vscode创建项目确保为framework 版本默认即可,如果选择sta

android (No cached version available for offline mode)----bug解析处理

错误日志 Execution failed for task ':base:generateDebugRFile'.> Could not resolve all files for configuration ':base:debugCompileClasspath'.> Could not download core-1.3.0.aar (androidx.core:core:1.3.0)