This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Project source code address: github.com/Dr-Water/fa…
Springfox default will put all the API is divided into a group, so through similar to http://127.0.0.1:8080/jadDemo/swagger-ui.html address access, will be in the same page to load all the API list. Thus, if the system is a little larger and has a little more API, pages will appear to be dead, so it is necessary to group the API. The API group is defined by the @bean annotation in the ApiConf configuration file. The common configuration on the web is as follows:
@EnableSwagger2
publicclass ApiConfig {
@Bean
public Docket customDocket(a) {
returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); }}Copy the code
Injecting a Docket via @bean is not required. Without this configuration, the framework will generate a default Docket instance of its own. The purpose of this Docket instance is to specify public information about all the apis it can manage, such as the API version, author, and other basic information, and specify which apis to list only (filtered by API addresses or annotations).
There can be more than one Docket instance, such as this code:
package com.ratel.json.config;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
/** * @ Service description: *@package_name: com. Ratel. Json. Config *@project_name: fast - json - test *@author: [email protected] *@create_time: 2019-09-17 17:27
* @copyright(c) Ratelfu All Rights Reserved */
@Configuration
@EnableSwagger2
public class Swagger2GroupConfig {
/** * Whether to enable swagger, formal environment generally need to close, use@valueThe annotation gets the attribute */ from application.yml
@Value("${swagger.enabled}")
private boolean enableSwagger;
@Bean
public Docket createRestApiGroup1(a) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("apiGroup1")
.apiInfo(apiInfo())
// Enable or not (true enable false hide. Production environment recommended hidden)
.enable(enableSwagger)
.select()
// Class annotated with ApiOperation generates interface documentation
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// The interface document is generated for the class under the package
//.apis(RequestHandlerSelectors.basePackage("com.ratel.controller"))
.paths(PathSelectors.ant("/user/**"))
.build()
.securitySchemes(security());
}
@Bean
public Docket createRestApiGroup2(a) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("apiGroup2")
.apiInfo(apiInfo())
// Enable or not (true enable false hide. Production environment recommended hidden)
.enable(enableSwagger)
.select()
// Class annotated with ApiOperation generates interface documentation
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// The interface document is generated for the class under the package
//.apis(RequestHandlerSelectors.basePackage("com.ratel.controller"))
.paths(PathSelectors.ant("/shop/**"))
.build()
.securitySchemes(security());
}
private ApiInfo apiInfo(a) {
return new ApiInfoBuilder()
.title("Build RESTful APIs with Swagger2 in Spring Boot")
.description("More Spring Boot related articles please attention: https://blog.csdn.net/weter_drop")
.termsOfServiceUrl("https://blog.csdn.net/weter_drop/")
.contact("ratel")
.version("1.0")
.build();
}
/** * Add swagger's security-related parameters, such as token * in the request header@return* /
private List<ApiKey> security(a) {
return newArrayList(
new ApiKey("token"."token"."header")); }}Copy the code
When you have multiple Docket instances configured in your project, you can group the apis, such as the code above that divides the apis into two groups. In this case, you must give each group a different name, such as “apiGroup1” and “apiGroup2” in the code above, and each group can use Paths to specify which group manages which apis through Ant style address expressions. For example, in the configuration above, the first group manages apis starting with /user/ and the second group manages apis starting with /shop/. Of course, there are many other filtering methods, such as following data class annotations, method annotations, address regular expressions, and so on. You can select different API groups from the drop-down list in the upper right corner of the API list page. This splits the list of project apis into different pages. This way, it is easy to manage, but the page does not need to load too many apis to fake death.