How to use swagger and knife in Springboot
Springboot in how to use swagger and knife, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Swagger2 dependency package compile group: 'io.springfox', name:' springfox-swagger2', version: '2.9.2'
Compile group: 'io.springfox', name:' springfox-swagger-ui', version: '2.9.2'
/ / the following two packages are used to prevent warning logs thrown when comments are not given default values.
Compile group: 'io.swagger', name:' swagger-annotations', version: '1.5.22'
Compile group: 'io.swagger', name:' swagger-models', version: '1.5.22'
Configure @ Getter
@ Setter
@ Configuration
@ EnableSwagger2
@ ConfigurationProperties ("swagger")
Public class SwaggerConfig {
Private String host;// Custom host
@ Bean
Public Docket createRestApi () {
Return new Docket (DocumentationType.SWAGGER_2)
.apiInfo (apiInfo ())
.select ()
.apis (RequestHandlerSelectors.basePackage ("com.only.oc.web.api")) / / scan package
.apis (RequestHandlerSelectors.withClassAnnotation (Api.class)) / / scan the contorller annotated in API
.apis (RequestHandlerSelectors.withMethodAnnotation (ApiOperation.class)) / / scan the method with ApiOperation annotations
.notify (PathSelectors.any ())
.build () .host (host); / / Custom host
}
Private ApiInfo apiInfo () {
String version = DateUtil.customFormat (new Date (), "yyyyMMdd.HHmmss")
Return new ApiInfoBuilder ()
.title ("Onstand classroom (Business Edition)")
Description ("Enli classroom second edition, upgraded business modules on SDK version, business modules include: users, teachers, students, courses, classes, class statistics, etc.")
.version ("v2." + version)
.license ("Apache License Version 2.0")
.licenseUrl ("https://www.apache.org/licenses/LICENSE-2.0")"
.contact (new Contact ("Zhu Zengliang", "zhuzengliang@onlyedu.com"))
.build ()
}
}
Visit
Default access path http://locahost:8080/swagger.html
Knife4j dependency package compile group: 'com.github.xiaoymin', name:' knife4j-spring-boot-starter', version: '2.0.4'
Configuration
Add comments to the original SwaggerConfig class:
@ EnableKnife4j
@ Import (BeanValidatorPluginsConfiguration.class)
Visit
Default access path http://locahost:8080/doc.html
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.