1. 程式人生 > 其它 >介面文件從Swagger升級成knife4j使用教程(springboot)

介面文件從Swagger升級成knife4j使用教程(springboot)

一、Knife4j是一個為Swagger介面文件賦能的工具

二、引入pom

<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>

三、配置類
@Configuration
@EnableSwagger2
public class Knife4jConfiguration {

@Bean(value = "defaultApi3")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("Spring Security JWT 整合demo")
.description("# swagger-bootstrap-ui-demo RESTful APIs")
.termsOfServiceUrl("http://www.xx.com/")
.contact(new Contact("Kevin Yu", "http://example.com", "[email protected]"))
.version("1.0")
.build())
//分組名稱
.groupName("3.X版本")
.select()
//這裡指定Controller掃描包路徑
.apis(RequestHandlerSelectors.basePackage("com.ycs.study.web.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}

private ApiInfo apiInfo(String title, String desc) {
return new ApiInfoBuilder()
.title(title)
.description(desc)
//服務條款網址
.termsOfServiceUrl("http://127.0.0.1:9091/cim-sgsc")
.version("1.0")
.contact(new Contact("Kevin Yu", "", "[email protected]"))
.build();
}
}
四、api
@Slf4j
@ApiSupport(order = 3)
@Api(tags = "Index服務", value = "該引數沒什麼意義,在UI介面上也看到,所以不需要配置")
@RestController("/index")
public class IndexController {

@ApiOperationSupport(order = 33)
@ApiOperation(value = "get請求", notes = "111")
@ApiImplicitParams( {
@ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "age", value = "年齡", required = true, dataType = "int", paramType = "query")
})
@RequestMapping("/get")
public String get(String name, int age) {
return name + age;
}

@ApiOperation(value = "重新整理token", notes = "")
@GetMapping("/query")
public String query(){
return "query";
}

@PostMapping(value = "/postTest", produces = "application/json; charset=utf-8")
public String postTest(String name){
return "name";
}

}

五、請求路徑
http://localhost:8088/doc.html#/home