首页 > 其他分享 >【WEEK14】 【DAY3】Swagger Part 1【English Version】

【WEEK14】 【DAY3】Swagger Part 1【English Version】

时间:2024-06-02 17:30:36浏览次数:27  
标签:springfox end WEEK14 Swagger DAY3 Version 16.2 import swagger

2024.5.29 Wednesday

Contents

16.Swagger

Learning Objectives

  • Understand the concept and function of Swagger
  • Understand front-end and back-end separation
  • Integrate Swagger in Spring Boot to automatically generate API documentation

16.1.Introduction to Swagger

16.1.1.Front-End and Back-End Separation

Mainstream solution: Vue+SpringBoot
Back-end era: The front end only managed static pages; html ==> back end. Template engine JSP => back end was the main force.

16.1.2.The Era of Front-End and Back-End Separation

  • Front end -> Front-end control layer (two-way binding), view layer
    • Use JSON to fake back-end data. This means the front-end project can still run without the back end.
  • Back end -> Back-end control layer, service layer, data access layer
  • Front end and back end interact through APIs
  • Front end and back end are relatively independent and loosely coupled, and can even be deployed on different servers

16.1.3.Issues Arising

Front-end and back-end integration debugging, front end or back end cannot “negotiate in time, resolve early”, ultimately leading to a concentration of problems

16.1.4.Solution

  • First define the schema [planned outline], and track the latest API in real-time to reduce integration risks;
  • In the early years: specified Word planning documents;
  • Front-end and back-end separation:
    • Front end tests back-end interfaces: Postman
    • Back end provides interfaces, needs to update the latest messages and changes in real-time

16.1.5.Swagger

  • Claimed to be the world’s most popular API framework
  • Restful API documentation online auto-generator => API documentation and API definition updated synchronously
  • Direct operation, online testing of APIs
  • Supports multiple languages (e.g., Java, PHP, etc.)
  • Official website: https://swagger.io/

16.2.Integrating Swagger into Spring Boot

Integrating Swagger into Spring Boot => springfox, two jar packages
Springfox-swagger2
springfox-swagger-ui
Using swagger2: jdk version should be 1.8+

16.2.1.Create a new swagger-demo project

Insert image description here
Insert image description here

Add Maven support (click the plus sign to add the corresponding project in Project Structure->Modules),
Insert image description here

As usual, modify the Maven, jdk, and Java versions in settings, and the jdk and Java versions in Project Structure. Modify the Spring Framework version in the pom to 2.7.13, add Thymeleaf dependencies in the pom.xml, and reload Maven.
Delete unnecessary files
Insert image description here

16.2.2.Import Dependencies

Insert image description here
Insert image description here

16.2.2.1.springfox-swagger2

Maven Repository: io.springfox » springfox-swagger2 » 2.9.2 (mvnrepository.com)

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
16.2.2.2.springfox-swagger-ui

Maven Repository: io.springfox » springfox-swagger-ui » 2.9.2 (mvnrepository.com)

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
16.2.2.3.pom.xml

Current complete configuration file

<?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.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>swagger-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>swagger-demo</name>
    <description>swagger-demo</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>

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

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</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>

16.2.3. Create a controller folder

16.2.3.1. Create HelloController.java

Insert image description here

package com.P47.controller;

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";
    }
}
16.2.3.2. Run SwaggerDemoApplication.java

Default URL: /error
Test HelloController: http://localhost:8080/hello
Insert image description here

16.2.4. Configure Swagger

16.2.4.1. Create a config folder, SwaggerConfig.java

Insert image description here

package com.P47.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration  // Equivalent to @Component
@EnableSwagger2 // Enable Swagger2
public class SwaggerConfig {
}
16.2.4.2. Solution for http://localhost:8080/swagger-ui.html not opening

See the blog https://blog.csdn.net/2401_83329143/article/details/138858363
Modify application.properties

spring.application.name=swagger-demo
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
16.2.4.3. Rerun SwaggerDemoApplication.java

http://localhost:8080/swagger-ui.html
Insert image description here

16.3. Configure Swagger

The Swagger instance Bean is Docket, so configure Swagger through the Docket instance.

16.3.1. Modify SwaggerConfig.java

package com.P47.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;

@Configuration  // Equivalent to @Component
@EnableSwagger2 // Enable Swagger2
public class SwaggerConfig {
    // Configure the Swagger bean instance Docket to set up Swagger's specific parameters
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)  // Source code in DocumentationType.class, choose the version that fits
                .apiInfo(apiInfo()); // Method public Docket(DocumentationType documentationType), click on ApiInfo to enter ApiInfo.class
        // Click to enter Docket.class to see the source code of various methods
    }

    // Configure Swagger information (apiInfo)
    private ApiInfo apiInfo(){

        // Prevent DEFAULT_CONTACT (renamed to contact) from reporting errors
        Contact 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 URL
                contact,    // Contact information
                "Apache 2.0",   // License
                "http://www.apache.org/licenses/LICENSE-2.0",   // License URL
                new ArrayList<>() // Extensions
        );
    }
}

16.3.2. Restart

http://localhost:8080/swagger-ui.html#/
Insert image description here
Insert image description here
The source code of this page is located at: D:\apache-maven-3.5.4\repository\io\springfox\springfox-swagger-ui\2.9.2\springfox-swagger-ui-2.9.2.jar!\META-INF\resources\swagger-ui.html

标签:springfox,end,WEEK14,Swagger,DAY3,Version,16.2,import,swagger
From: https://blog.csdn.net/2401_83329143/article/details/139349689

相关文章

  • 【WEEK14】 【DAY5】Swagger第三部分【中文版】
    2024.5.31Friday接上文【WEEK14】【DAY4】Swagger第二部分【中文版】目录16.6.配置API分组16.6.1.修改SwaggerConfig.java16.6.2.重启16.7.实体配置16.7.1.新建pojo文件夹16.7.2.修改HelloController.java16.7.3.重启16.8.常用注解16.8.1.Swagger的所有注解定义在i......
  • AndroidStudio升级Gradle到7+,compileSdkVersion 33+
    一、概述由于需求方的要求/需要,主动或被动的需要升级android的编译环境到CompileSdkVersion33。此时直接更改android项目的编译版本会报错,as版本过低或者gradle插件太老了等。也会遇到一些这样那样的bug,这一篇做一下简单的总结升级方式:以更......
  • 【WEEK14】 【DAY2】Shiro Part 7【English Version】
    2024.5.28TuesdayContinuationfromprevious【WEEK14】【DAY1】ShiroPart6【EnglishVersion】Contents15.8.IntegrateShirowithThymeleaf15.8.1.Modifypom.xmltoAddDependencies15.8.1.1.Importingtheshiro-thymeleafIntegrationPackage15.8.1.2.......
  • 【Java】 如何解决Java中的UnsupportedClassVersionError错误
    >>【痕迹】QQ+微信朋友圈和聊天记录分析工具>>(1)纯Python语言实现,使用Flask后端,本地分析,不上传个人数据。>>(2)内含QQ、微信聊天记录保存到本地的方法,真正实现自己数据自己管理。>>(3)数据可视化分析QQ、微信聊天记录,提取某一天的聊天记录与大模型对话。>>**下载......
  • 《python编程从入门到实践》day39加更
    #昨日知识点回顾    添加主题、条目#今日知识点学习    19.1.3编辑条目        1.URL模式edit——entry#learning_logs/urls.py---snip---#用于编辑条目的页面path('edit_entry/<int:entry_id>/',views.edit_entry,na......
  • Day3_beast实现http server
    一、绑定和监听连接在1CServer.h中声明acceptor,以及用于事件循环的上下文iocontext,和构造函数classCServer:publicstd::enable_shared_from_this<CServer>{public:CServer(boost::asio::io_context&ioc,unsignedshort&port); //构造函数voidStart(); /......
  • Day3
    今天是day3,今天的内容如下:第一题为移除链表元素,简而言之为,给定一个链表,要求删除所有值为val的节点并最终把头结点输出。funcremoveelements(head*ListNode,valint)*ListNode{forhead!=nil&&head.Val==val{head=head.next}cur:=headfor......
  • Day36 代码随想录打卡|二叉树篇---翻转二叉树
    题目(leecodeT226):给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。方法:迭代法翻转二叉树,即从根节点开始,一一交换每个节点的左右孩子节点,然后递归此过程,将根节点的左右孩子节点再分别作为参数传入交换节点的函数中。重复此过程,直到结束。就完成了二叉树的翻......
  • DcatAdmin 多对多关联是,multipleSelect 报错 Array to string conversion
    classActivityextendsModel{//定义关联关系publicfunctionadminUsers(){//中间表$pivotTable='activity_admin_users';//关联模型类名$relatedModel=AdminUser::class;return$this->belongsToMany($......
  • 前端面试题日常练-day33 【面试题】
    题目希望这些选择题能够帮助您进行前端面试的准备,答案在文末。在jQuery中,以下哪个选项用于在元素上绑定一个点击事件?a)click()b)bind()c)on()d)trigger()jQuery中,以下哪个选项用于获取元素的属性值?a)text()b)html()c)val()d)attr()在jQuery中,以下哪......