Give swagger a new skin knife4j integrated record
Swagger has a set of classic UI, but it is not very easy to use. I saw knife4j before, with beautiful interface and perfect functions, so I tried integration.
Demo reference example address: knife4j spring boot demo
Knife4j, formerly known as swagger bootstrap UI, is a tool for enabling swagger interface documents
According to official documents, integration is very convenient.
Maven reference
The first step is in the project POM The dependencies introduced knife4j in the XML file are as follows:
<dependencies>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
The latest version is 2.0 six
If you want to use BOM to import, please refer to Maven BOM reference
Create swagger profile
Create a new swagger configuration file swaggerconfiguration Java file to create the docket grouping object provided by springfox. The code is as follows:
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//分组名称
.groupName("2.X版本")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.swagger.bootstrap.ui.demo.new2"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
The above two notes need special explanation, as shown in the table below:
Spring security authentication free
Add / * * / doc HTML / * * add:
private String[] getSwaggerUrl() {
List<String> urls = new ArrayList<String>();
urls.add("/**/swagger-resources/**");
urls.add("/**/webjars/**");
urls.add("/**/doc.html/**");
urls.add("/**/v2/**");
urls.add("/**/swagger-ui.html/**");
return urls.toArray(new String[urls.size()]);
}
http.authorizeRequests()
.antMatchers(getSwaggerUrl()).permitAll()
Test access
Enter the address in the browser: http://host:port/doc.html
You can set global parameters:
Support online debugging
Offline documents support exporting MD, PDF, etc
last
How can the front end call the API more gracefully? reference resources:
Vue uses typescript to call swagger API gracefully
If you have time later, you can integrate this into knife4j