SpringBoto集成Knife4j
添加maven,可以去官方找最新的
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
创建Swagger配置依赖
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfigruation {
@Bean(value = "defaultApi2")
public Docket defaultApi2(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("demo1") //标题
.description("# swagger-bootstrap-ui-demo RESTful APIs") //描述
.termsOfServiceUrl("http://www.xx.com")
.version("1.0")
.build())
//分组名称
.groupName("2.x版本")
.select()
//这里指包的路径
.apis(RequestHandlerSelectors.basePackage("com.example.demo.cotroller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
整个工程
DomeController和TextController测试接口
/**
* 测试类
*/
@Api(tags = "首页模块")
@RestController
public class TextController {
/**
* 首页
* @return String
*/
@ApiImplicitParam(name = "name", value = "姓名", required = true)
@ApiOperation("向客人问好")
@PostMapping("index")
public String index(String name){
if(!name.equals(""))
return "holle "+name;
return "holle world";
}
}
/**
* 测试
*/
@Api(tags = "测试")
@RestController
public class DomeController {
/**
*
* @param num
* @return String
*/
@ApiImplicitParam(name = "num",value = "数字",required = true)
@ApiOperation("像世界问好")
@GetMapping("show")
public String show(int num){
return num+"你好 欢迎使用Knife4j";
}
}
然后启动spring boot工程 访问地址:http://localhost:8080/doc.html
这样就ok了