【WEEK14】 【DAY5】Swagger Part 3【English Version】

2024.5.31 Friday
Following up on【WEEK14】 【DAY4】Swagger Part 2【English Version】

Contents

  • 16.6. Configure API Groups
    • 16.6.1. Modify SwaggerConfig.java
    • 16.6.2. Restart
  • 16.7. Entity Configuration
    • 16.7.1. Create a new pojo folder
    • 16.7.2. Modify HelloController.java
    • 16.7.3. Restart
  • 16.8. Common Annotations
    • 16.8.1. All Swagger annotations are defined in the io.swagger.annotations package
    • 16.8.2. We can also configure some annotations for the request interfaces (modify HelloController.java)
    • 16.8.3. Restart

16.6. Configure API Groups

16.6.1. Modify SwaggerConfig.java

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
    @Bean
    public Docket docket(Environment environment){

        // Set the environments where Swagger should be displayed
        Profiles profiles = Profiles.of("dev", "test");
        // Determine if the current environment matches the profiles
        // Use enable() to accept this parameter and decide whether to display
        boolean flag = environment.acceptsProfiles(profiles);
        flag=true;  // To access through port 8080, and because the dev and test profile ports do not include 8080, manually set flag to true to ensure enable(flag) starts normally.

        return new Docket(DocumentationType.SWAGGER_2)  // See the source code in DocumentationType.classpublic class DocumentationType extends SimplePluginMetadata method, 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.
                .groupName("ZzzZzzzZzzzz-") // Rename the default group to a custom name

                .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 multiple groups, when accessing these groups in the page, the page is set to the default settings: because no other configuration is done
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }

    // Configure Swagger information (apiInfo)
    ......
}

16.6.2. Restart

Insert image description here
Select group1, group2, and group3 respectively
Insert image description here
Insert image description here
Insert image description here

16.7. Entity Configuration

16.7.1. Create a new pojo folder

Create User.java
Insert image description here

@ApiModel adds annotations to the class
@ApiModelProperty adds annotations to the class properties
package com.P47.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("User Entity")
public class User {
    @ApiModelProperty("Username")
    public String username;
    @ApiModelProperty("Password")
    public String password;
}

16.7.2. Modify HelloController.java

package com.P47.controller;

import com.P47.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping("/getUser")
    public User getUser(){
        return new User();  // The User.java just created is the return value of the getUser request interface, so it can be mapped to the entity item (which can be displayed in the Models section on the page)
    }
}

As long as this entity is in the return value of the request interface (even if it is a generic type), it can be mapped to the entity item.
Note: It is not because of the @ApiModel annotation that the entity is displayed here, but because any entity that appears in the return value of the interface method will be displayed here. The @ApiModel and @ApiModelProperty annotations are only used to add comments to the entity.

16.7.3. Restart

Insert image description here

16.8. Common Annotations

16.8.1. All Swagger annotations are defined in the io.swagger.annotations package

Here are some commonly used ones. Those not listed can be referred to in the documentation:

Swagger AnnotationBrief Description
@Api(tags = “xxx module description”)Used on module classes
@ApiOperation(“xxx interface description”)Used on interface methods
@ApiModel(“xxx POJO description”)Used on model classes: such as VO, BO
@ApiModelProperty(value = “xxx property description”, hidden = true)Used on class methods and properties, hidden can hide the property
@ApiParam(“xxx parameter description”)Used on parameters, methods, and fields, similar to @ApiModelProperty

16.8.2. We can also configure some annotations for the request interfaces (modify HelloController.java)

package com.P47.controller;

import com.P47.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

@Api(tags = "Text explanation for HelloController")
@RestController
public class HelloController {

    @GetMapping(value = "/hello")   // Changed from RequestMapping
    public String hello(){
        return "hello";
    }

    @PostMapping("/getUser")    // Changed from RequestMapping
    public User getUser(){
        return new User();  // The User.java just created is the return value of the getUser request interface, so it can be mapped to the entity item (which can be displayed in the Models section on the page)
    }

    @ApiOperation("Annotation of the interface")
    @PostMapping("/trytry")
    @ResponseBody
    public String Zzz(@ApiParam("The returned name annotation is written here") String usernameaa){
        return usernameaa;
    }
}

16.8.3. Restart

Insert image description here
Insert image description here
You can click “try it out” in the upper right corner to test.
For example:
Insert image description here
Insert image description here
Advantages: It can add some configuration information to attributes or interfaces that are difficult to understand, making them easier to read!
Compared with the traditional way of testing interfaces with Postman or Curl, using swagger is simply foolproof. It does not require additional documentation (well-written documentation itself) and is less prone to errors. You only need to enter the data and click Execute. If combined with an automated framework, it can be said that no manual operation is needed at all.
Swagger is an excellent tool, and many small and medium-sized internet companies in China are already using it. Compared with the traditional way of writing a Word interface document first and then testing, this approach is more in line with the current rapid iterative development trend. Of course, remember to turn off Swagger in the production environment for security reasons and to save runtime memory.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

RDD与Java实战:学生列表,先按性别降序,再按年龄降序排列

文章目录 Scala RDD 实现Java 实现实战总结 在本实战任务中&#xff0c;我们的目标是对学生列表进行排序&#xff0c;排序规则是先按性别降序排列&#xff0c;再按年龄降序排列。我们提供了两种实现方式&#xff1a;使用Scala的RDD&#xff08;弹性分布式数据集&#xff09;和…

牛客网刷题 | BC109 正斜线形图案

目前主要分为三个专栏&#xff0c;后续还会添加&#xff1a; 专栏如下&#xff1a; C语言刷题解析 C语言系列文章 我的成长经历 感谢阅读&#xff01; 初来乍到&#xff0c;如有错误请指出&#xff0c;感谢&#xff01; 描述 KiKi学习了循环&am…

今日好料推荐(这就是会计)

今日好料推荐&#xff08;这就是会计&#xff09; 参考资料在文末获取&#xff0c;关注我&#xff0c;获取优质资源。 这就是会计&#xff1a;资本市场的会计逻辑 是一本由中国会计专家编写的书籍&#xff0c;旨在深入探讨会计在资本市场中的核心作用及其运作逻辑。作为一本…

HTTPS加密

一.加密是什么 加密就是把明文(要传输的信息)进行一系列的变换,生成密文. 有加密就有解密,解密就是把密文进行一系列的变换,生成明文. 在这个加密和解密过程中,往往需要一个或多个中间数据,辅助进行这个过程,这样的数据称为密钥. 加密解密到如今已经发展成了一个独立的学科 : 密…

AtCoder Beginner Contest 356 A~E(F更新中...)

A.Subsegment Reverse 题意 给出三个正整数 N , L , R N, L, R N,L,R。 对于一个序列 A ( 1 , 2 , … , N ) A (1, 2, \ldots, N) A(1,2,…,N)&#xff0c;请你输出翻转了 L ∼ R L \sim R L∼R之间数字后得到的序列。 分析 使用循环进行翻转即可。 代码 #include <…

学工管理系统有什么作用

学生信息办理系统是针对学校学生处的很多作业处理作业而开发的办理软件&#xff0c;首要用于学校学生信息办理&#xff0c;整体使命是完成学生信息联系的体系化、科学化、标准化和自动化&#xff0c;其首要使命是用手机和计算机对学生各种信息进行日常办理&#xff0c;如查询、…

Ubuntu 20.04 LTS配置JDK、Git

一、配置JDK 1.1 更新系统 执行以下命令 sudo apt update 出现以下界面即为安装成功 1.2 安装openjdk-11-jdk Ubuntu20.04中没有默认JDK&#xff0c;执行以下指令安装&#xff0c;默认会自动配置一些必要环境变量 sudo apt install openjdk-11-jdk 1.3 配置环境变量&…

CMake编译安装、生成可执行程序、生成静态动态库以及静态动态库的链接

1 CMake介绍 CMake是一个开源的、跨平台的构建系统&#xff0c;用于管理软件从源代码到可执行文件的整个构建过程。它最初由Kitware公司为ITK&#xff08;Insight Segmentation and Registration Toolkit&#xff09;和VTK&#xff08;Visualization Toolkit&#xff09;等开源…

TimeDao-一篇文章了解清楚Subspace项目

1 项目简介 什么是Subspace网络&#xff1f; Subspace是为下一波加密创建者构建的第四代区块链。旨在实现web3规模扩容。 Subspace允许开发者以互联网规模运行 Web3 应用。它提供了一个简单的接口&#xff0c;用于快速部署按需求自动扩展的多链去中心化应用。Subspace由一个…

Python06 条件判断语句

Python 条件判断语句 Python 条件判断语句格式1if 条件 :else:格式2if 条件 :elif条件 :else:三目: second_max num1 if 条件语句 else num2# 快捷键: tab 整体向右移动一个水平制表符&#xff0c;shift tab 整体向左移动一个水平制表符 num1 10 num2 20 if num2 > num…

每日5题Day15 - LeetCode 71 - 75

每一步向前都是向自己的梦想更近一步&#xff0c;坚持不懈&#xff0c;勇往直前&#xff01; 第一题&#xff1a;71. 简化路径 - 力扣&#xff08;LeetCode&#xff09; class Solution {public String simplifyPath(String path) {Deque<String> stack new LinkedList…

Java17 --- SpringCloud之seate

目录 一、创建seata需要的mysql数据库表 二、修改seata的配置文件 三、启动nacos及seata 四、创建需要的数据库及表 一、创建seata需要的mysql数据库表 CREATE DATABASE seata;CREATE TABLE IF NOT EXISTS global_table(xid VARCHAR(128) NOT NULL,…

电影APP需求规格说明书示范

电影APP需求规格说明书示范 目录结构参考1 引言1.1编写目的1.2背景1.3项目目标1.4 概述 2 整体说明2.1 用例模型2.2 产品功能2.3 用户特点2.4 需求分配 3 具体需求3.1用例描述3.2用例细化 4 支持信息 目录结构参考 计算机软件需求规格说明规范 标准号&#xff1a;GB/T 9385-20…

Jmeter参数化

Jmeter参数化 本质&#xff1a;使用参数的方式来替代脚本中的固定的测试数据 实现方式&#xff1a; 定义变量&#xff08;最基础&#xff09; 文件定义的方式&#xff08;所有测试数据都是固定的情况下&#xff09; 数据库的方式&#xff08;灵活&#xff09; 函数方式&am…

详解 Spark核心编程之广播变量

广播变量是分布式共享只读变量 一、广播变量功能 ​ 广播变量用来将一个较大的数据对象发送到 Executor 并保存在内存中&#xff0c;同一个 Executor 中的所有 Task 都可以读取且只能读取广播变量中的数据&#xff0c;从而达到共享的目的&#xff0c;避免 Executor 中存在大量…

java—MyBatis框架

简介 什么是 MyBatis&#xff1f; MyBatis 是一款优秀的持久层框架&#xff0c;它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO&…

SparkSql近期使用经验分享

背景 近期在公司使用了SparkSql重构一个由Java开发的ETL程序&#xff0c;因为Java模块不易于修改和部署&#xff0c;而由于SparkSql脚本是由Python开发&#xff0c;便于根据业务需求来开发维护&#xff0c;特别是不需要编译、打包部署。 技术理念 SparkSql是以Sql的形式去开…

三十三篇: 解锁决策之门:专家系统深度探索与未来展望

解锁决策之门&#xff1a;专家系统深度探索与未来展望 在今天这个日益复杂的世界中&#xff0c;我们对决策的速度和质量提出了更高的要求。在众多解决方案中&#xff0c;专家系统作为人工智能的一大分支&#xff0c;扮演着不可或缺的角色。它不仅是技术创新的产物&#xff0c;…

html+CSS+js部分基础运用11

一、改变新闻网页中的字号 1、设计如图1-1所示的界面&#xff0c;要求当网络访问者选择字号中的【大、中、小】时能实现页面字号大小变化&#xff0c;选择“中”时&#xff0c;页面效果如图1所示。 图1 单击前初始状态页面 图2 单击“中”链接后页面 2、div中内容如下&#x…

操作系统|进程和线程的上下文以及他们的上下文切换具体流程?

进程和线程已经是老生常谈的问题了&#xff0c;现在那么他们是如何进行切换的呢&#xff1f;他们之间的切换有什么区别呢&#xff1f;如果你不懂的话&#xff0c;就让我们一起来探讨一下吧&#xff01; 进程上下文切换(context switch) 进程到底由哪些部分组成&#xff1f; …