在用postman做接口测试的时候,如果字段比较多,且对象比较多的时候,参数的填写就很耽误时间,Knife4j一切都为你自动生成,你只需要填写参数值就好了。简直不要太方便。
下面动手实践一下吧
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.4</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
api配置类:
package com.awei.cloud.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
/**
* 这里配置swagger扫描的包
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.awei.cloud"))
.paths(PathSelectors.any()).build();
}
/**
* 这里配置swagger对外提供服务的端口
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("阿伟自测接口")
.description("简直神器")
// .termsOfServiceUrl("http://127.0.0.1:8081/doc.html")
.version("1.0").build();
}
}
http://127.0.0.1:8081/doc.html,直接访问。
感谢 溪源的奇思妙想 大佬的启发。