第一步:
在maven项目的pom.xml中引入Knife4j的依赖包
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
第二步:创建Knife4j配置Knife4jConfiguration
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.version("1.0")
.title("测试服务")
.description("<div style='font-size:15px;color:red;'>测试服务接口</div>")
.build())
//分组名称
.groupName("1.0 版本")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.SF.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
第三步:使用
@Api(tags = "首页模块")
@RestController
public class IndexController {
@ApiImplicitParam(name = "name",value = "姓名",required = true)
@ApiOperation(value = "hello")
@GetMapping("/hello")
public ResponseEntity<String> hello(@RequestParam(value = "name")String name){
return ResponseEntity.ok("Hi:"+name);
}
}