神器:Knife4j ,太方便了,大愛。代替postman

大齡畢業生發表於2020-10-15

在用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("釋出模擬boos介面")
                .description("簡單優雅的釋出模擬boos介面restful風格介面")
                // .termsOfServiceUrl("http://127.0.0.1:8081/doc.html")
                .version("1.0").build();
    }
}

http://127.0.0.1:8081/doc.html,直接訪問。
在這裡插入圖片描述
在這裡插入圖片描述

相關文章