“This is the sixth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

๐Ÿ‘จ๐ŸŽ“ Author: Bug Bacteria

โœ๏ธ blog: CSDN, Nuggets, etc

๐Ÿ’Œ public account: Magic House of the Circle of the Apes

๐Ÿšซ special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

๐Ÿ™ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

Friends in the process of reviewing the article if you feel that the article has a little help, please don’t be mean about your hand like it, bold to light up the article ๐Ÿ‘, your like three even (collect โญ๏ธ+ pay attention to ๐Ÿ‘จ ๐Ÿ‘จ + comment on the board) is the best encouragement and support for bug bacteria on my creation road. Time does not abandon, creation, refueling!

A: background

Today, I would like to introduce you to a unique good thing, you have not used friends after experience will absolutely love, from now on free our hands, true true true absolutely absolutely son, must amway to you.

In the daily development process, there must be many partners have been written interface document poisoning, finally write the document, the front end and ran to complain about the interface document is inconsistent with the actual situation, and then they often indulge in writing and maintaining interface documents can not extricate themselves, often too late to update, caused this kind of episode. But interface documentation for us, just like comments, although we often complain that other people write code without comments, but their own code, the most annoying, is to write comments (ha ha ha said everyone’s mind).

So it’s not enough just to standardize by force. As time goes on and versions are iterated, it’s easy for interface documentation to keep up with the updates to the code interface, and it’s easy to suffer ridicule and unnecessary trouble if you don’t synchronize.

So, what’s the best way to get out of this mess? Please read on.

Introduction to Swagger

I’m not going to keep you in suspense, I’m sure many of you have already used it, but it doesn’t matter, as long as there is one person in the world who hasn’t used it, I will tell him.

So what is it? That’s it, Swagger!

1. What is Swagger?

Swagger is a canonical and complete framework for generating, describing, invoking, and visualizing RESTful Web services. It is also the world’s largest OpenAPI Specification (OAS) API development tool framework, and supports the development of the entire API life cycle from design and documentation to testing and deployment.

It was created to enable the client and file system to update at the same rate as the server. File methods, parameters, and models are tightly integrated into server-side code, allowing the API to always be in sync.

2. Why Swagger?

Swagger’s goal is to define a standard, language-neutral interface for REST APIs that enables people and computers to discover and understand the capabilities of various services without access to source code or documentation or network traffic detection.

When the service is defined by Swagger, the consumer can interact with the remote service with a little implementation logic. Like a low-level programming interface, Swagger takes a lot of the guesswork out of calling services.

What are the important characteristics of Swagger?

  • Code intrusive annotations
  • Follow the YAML document format
  • It is very suitable for API management of three terminals (PC, iOS and Android), especially suitable for the architecture mode of complete separation of front and back ends.
  • Reduce unnecessary documentation in line with agile development philosophy
  • Powerful.

Doesn’t it taste better than your handwritten interface document? (From the soul)

That said, certainly a lot of small partners are ready to move, eager to try, antsy, “so good, that can not quickly teach us, how to use ah?” .

Bug bacteria calmly replied: “don’t worry ha, let bug bacteria fill a hunger first, go back.”

Ten minutes passed…

Half an hour passed…

. .

An hour later…” All right, buddy, I’m coming. “The man finally came back.” Okay, let’s go on.”

3. Integrate Swagger

First integrate Swagger document, two steps need to be done, please proceed as follows:

1. Pom introduces the corresponding Swagger dependency package

<! --swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> The < version > 2.7.0 < / version > <! Annotations exclude transitive dependencies (default depends on the specified version of models and Annotations) Exclusion --> <exclusions> <groupId> IO. Swagger </groupId> <artifactId> Swagger - Annotations </artifactId> </exclusion>  <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <! --> <dependency> <groupId>com.github. Xiaoymin </groupId> <artifactId>swagger-bootstrap-ui</artifactId> The < version > 1.9.2 < / version > < / dependency > <! Annotations --> <dependency> <groupId> IO. Swagger </groupId> <artifactId> Annotations < version > 1.5.21 < / version > < / dependency > < the dependency > < groupId > IO. Swagger < / groupId > < artifactId > swagger - models < / artifactId > < version > 1.5.21 < / version > < / dependency >Copy the code

It looks like a bit much, but to be honest, it’s not much. After you experience it, you won’t think it’s anything. Since the native UI wasn’t pretty, I changed it, and you can choose it yourself.

This is my UI interface integrated with Xiaoymin open source: it is very beautiful. If you are curious, you can play the native UI by yourself. I will not give you a demonstration here

2. Configure the config file

Now that all dependencies are referenced, you need to configure them to specify several properties and access the Swagger INTERFACE URL.

package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; 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; /** * Swagger Configuration ** @author luoyong * @date 2021-06-01 13:00 */ @configuration // must exist @enablesWagger2 // must exist public class SwaggerConfig { @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) ApiInfo (apiInfo ()). The select () apis (RequestHandlerSelectors. BasePackage (" com. Example. Demo. Controller ")) / / controller directory you  .paths(PathSelectors.any()) .build(); } private ApiInfo ApiInfo () {return new ApiInfoBuilder().title(" Swagger API document ").contact(new) Contact("luoyong", "", Description ("swagger-bootstrap-ui") // Interface document description.termsofServiceurl ("http://localhost:8080") // Specify the url to access the online interface document .version("1.0") // Specify version number.build(); }}Copy the code

All configured, let’s restart the project, then the browser enter the url address: http://localhost:8080/doc.html

As you will see, this is the UI THAT I screenshot above. Yes, that means it’s already configured.

As for how to specifically combine your business interface for online presentation of documents, I will be in the next period for teaching, please look forward to it.

. .

OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.

Four, the past popular recommendation

How to integrate Swagger2 with Springboot

Springboot series 13: How to integrate Swagger2 online Interface document copy

Springboot series (12) : How to code the configuration of sending email reminders

Springboot series (12) : how to code a simple email send

Springboot series (12) : how to code the picture with attachment mail

Springboot series (12) : how to achieve the code to send a large copy of the mail – attached complete code

. .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ˜˜๏ธ Be who you want to be, there is no time limit, you can start whenever you want,

๐Ÿ€ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

โ€‹

๐Ÿ’“ If this article is helpful to you, please leave a like! (# ^. ^ #);

๐Ÿ’ if you like the article shared by bug fungus, please give bug fungus a point of concern! (เน‘ ‘แด— โ€ต เน‘);

๐Ÿ’— if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

๐Ÿ’ž In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

๐Ÿ’• copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you