Zfs-swagger-ui API document management (no independent test project needed to be built)

Keywords: Operation & Maintenance Spring Java Front-end

Brief Description: This project is a rewrite of swagger 2 UI page and a test project. It really won't write a brief description, but it's a direct topic.

Want to see the effect map.



How to use it?

Adding dependencies to pom files is as follows

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.zhangfusheng/zfs-swagger-ui -->
<dependency>
    <groupId>cn.zhangfusheng</groupId>
    <artifactId>zfs-swagger-ui</artifactId>
    <version>1.0.0</version>
</dependency>

The configuration class is as follows (ok by modifying the path of the scanned package)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@EnableSwagger2
@Configuration
@ComponentScan("cn.zfs")
public class SwaggerConfig extends WebMvcConfigurerAdapter {

    /**
     * If you configure the page access information in the configuration file, you need this method, otherwise you cannot access swagger-ui.html.
     *
     * @param registry --
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket createRestApi() {
        ArrayList<SecurityScheme> securitySchemes = new ArrayList<>();
        securitySchemes.add(new ApiKey("Set-Cookie", "Cookie", "header"));
        securitySchemes.add(new ApiKey("Authorization", "Authorization", "header"));
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).groupName("1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.zfs.swagger.ui.one.controller"))
                .paths(PathSelectors.any())
                .build().securitySchemes(securitySchemes);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot Use in Swagger2 structure Restful Api") //Setting the title of the document
                .description("Spring boot Swagger2 study: Spring boot Swagger2 study") //Set the description of the document - > 1. Overview
                .termsOfServiceUrl("Spring boot Swagger2 study") //Setting the Licence Information for Documents - > 1.3 License Information
                .contact("qhong")//Set Document Contact Mode - > 1.2 Contact Information
                .version("1.0")
                .build();
    }

    @Bean
    public Docket createRestApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo2()).groupName("2")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.zfs.swagger.ui.two.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo2() {
        return new ApiInfoBuilder()
                .title("Spring Boot Use in Swagger2 structure Restful Api") //Setting the title of the document
                .description("Spring boot Swagger2 study: Spring boot Swagger2 study") //Set the description of the document - > 1. Overview
                .termsOfServiceUrl("Spring boot Swagger2 study") //Setting the Licence Information for Documents - > 1.3 License Information
                .contact("qhong")//Set Document Contact Mode - > 1.2 Contact Information
                .version("1.0")
                .build();
    }

}

Access path

/ swagger-ui.html (native UI page)
/ swagger-zfs-ui.html (new version of UI page)

Need to introduce my jar package, jar package only front-end pages, do not want to quote, I have big tricks, look at my next blog!!! Brother has big tricks!

https://blog.csdn.net/qq_33547169/article/details/83275389

Posted by CraigRoberts on Sun, 27 Jan 2019 03:27:15 -0800