Learning goals

  • Understand the role and concept of Swagger
  • Understand front and rear end separation
  • Integrate Swagger in SpringBoot

1. Introduction of Swagger

Separation of front and rear ends

Vue+Springboot backend era: the front end only tube static page; HTML – > the back end. The template engine JSP = is the backbone of the back end

Separation before and after:

  • Back-end: Control layer DAO, Service layer Service, data access layer [back-end team]

  • Front end: Front end control layer, view layer [front end team] – Falsify back-end data: JSON already exists without the need for back-end data transfer

  • How do the front and back ends interact? = = = = “API

– The front and rear ends are relatively independent and loosely coupled

  • The front and back ends can be deployed on different servers.

There is a problem: – Front-end and back-end integration and joint adjustment, front-end and back-end personnel can not do “even if coordinated, solve as soon as possible”, resulting in the final outbreak of problems;

Solution:

  • First, specify schema [outline of the plan], update the latest API in real time, reduce the risk of integration;
  • Earlier years: Specify word project documents
  • Separation of front and rear ends:
    • Front-end test Back-end interface: Postman
    • The backend provides interfaces that need to be updated with the latest news and changes

Swagger

  • The most popular API framework in the world;
  • The Api document is automatically generated online => THE Api document is updated with the Api definition
  • Run directly, can test API interface online;
  • Supports multiple languages :(Java, Php…)

Liverpoolfc.tv: team | API documentation and design tools, Wagner (swagger. IO)

2. Using Swagger in a project requires a Springbox

  • swagger2
  • UI

Springboot integration Swagger

1. Create a Springboot project. 2Copy the code
< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 3.0.0 < / version > </dependency> <! -- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> < artifactId > springfox swagger - UI < / artifactId > < version > 3.0.0 < / version > < / dependency >Copy the code

3. Write a Hello control class

4. Configuration Swagger = = > config

5. Test the swagger

Configuration Swagger

Springfox provides a Docket object that allows us to flexibly configure Swagger properties. `@Configuration @EnableSwagger2 public class SwaggerConfig {

Public Docket API (){the DocumentationType class defines some static constants We can directly by the class the constants into the return new Docket (DocumentationType. SWAGGER_2); }Copy the code

DocumentationType source

public class DocumentationType extends SimplePluginMetadata {
  public static final DocumentationType SWAGGER_12 = new DocumentationType("swagger"."1.2");
  public static final DocumentationType SWAGGER_2 = new DocumentationType("swagger"."2.0");
  public static final DocumentationType SPRING_WEB = new DocumentationType("spring-web"."1.0");
  private final MediaType mediaType;

Copy the code

Document information

Swagger document information is used to describe and supplement the interface

As with SpringBoot, the default Swagger configuration is used if there is no documentation information for any configuration.

Docket source code snippet

// calls a constant as the default configuration for document information
private ApiInfo apiInfo = ApiInfo.DEFAULT;

Copy the code

DEFAULT Static constant

public static final ApiInfo DEFAULT = new ApiInfo("Api Documentation"."Api Documentation"."1.0"."urn:tos",
        DEFAULT_CONTACT, "Apache 2.0"."http://www.apache.org/licenses/LICENSE-2.0".new ArrayList<VendorExtension>());

Copy the code
Note:

Also like SpringBoot, if we call this method to configure the document information.

Swagger will use our documentation information and the default configuration will be invalid.

public Docket apiInfo(ApiInfo apiInfo) {
  this.apiInfo = defaultIfAbsent(apiInfo, apiInfo);
  return this;
Copy the code

The configuration document information needs to be passed in an instance of ApiInfo. Let’s click on the source code to see what parameters need to be passed in to build this instance.

public class ApiInfo {

  // Some static constants
    // contact is associated with the author
  public static final Contact DEFAULT_CONTACT = new Contact("".""."");
    // Some default values for swagger document information, which we see when we visit swagger-ui
  public static final ApiInfo DEFAULT = new ApiInfo("Api Documentation"."Api Documentation"."1.0"."urn:tos"./ / the default contact
          DEFAULT_CONTACT, "Apache 2.0"."http://www.apache.org/licenses/LICENSE-2.0".new ArrayList<VendorExtension>());
	// Version information
  private final String version;
    / / title
  private final String title;
    / / description
  private final String description;
    // Terms of Service website
  private final String termsOfServiceUrl;
    / / certificate
  private final String license;
    // Certificate address
  private final String licenseUrl;
    // Author related
  private final Contact contact;
    // Maybe it's a friendship link
  private final List<VendorExtension> vendorExtensions;

Copy the code

These constants can be configured to replace the default swagger configuration.

We’ll write an instance of the ApiInfo passed to the Docket ApiInfo (); In the method

@Configuration
@EnableSwagger2/ / open swagger2
public class SwaggerConfig {
    @Bean
    // Configure swagger's Docket Bean instance
    public Docket api(a){
        // Create the instance by passing in a DocumentationType
        The DocumentationType class defines static constants that we can pass in directly
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo());
    }

    // Configure Swagger information = APIInfo replace default configuration Remember that the above configuration calls this method
    public ApiInfo apiInfo(a)
    {
        Contact contact = new Contact("Yang Jing Li"."https://blog.csdn.net/weixin_45545764?spm=1001.2101.3001.5343"."[email protected]");
        return new ApiInfo(Swagger API."First test of Swagger."."1.0"."https://blog.csdn.net/weixin_45545764?spm=1001.2101.3001.5343",
                contact, "Apache 2.0"."http://www.apache.org/licenses/LICENSE-2.0".newArrayList<VendorExtension>()); }}Copy the code

Ok, after configuring the document information, let’s see what happens:

The document information is modified successfully

Interface filtering

/ / RequestHandlerSelectors, configuration to scan the way of the interface / / basePackage: specify to scan the package / / any () : to scan all / / none () : WithMethodAnnotation () scans only controllers that have the annotation reflection object classCopy the code
/ / RequestHandlerSelectors, configuration to scan the way of the interface / / basePackage: specify to scan the package / / any () : to scan all / / none () : WithMethodAnnotation () scans only controllers that have the annotation reflection object classCopy the code

[](13 messages) 【SpringBoot learning (nine) integrated document generation tool Swagger】_Moluuu blog -CSDN blog)