- 引入依赖
java <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
- 添加配置
```java @EnableSwagger2 @EnableKnife4j @Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("cn.ccb.controller")) .paths(PathSelectors.any()) .build().globalOperationParameters(pars); }
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.version(version)
.build();
}
} ```
- 编写controller
java @ApiOperation(value = "获取用户信息", produces = "application/json") @RequestMapping(value = "/base/getInfo", method = RequestMethod.GET) public Result getAllInfo () { userService.getAllInfo(); return Result.success("ok"); }
浏览器访问:http://localhost:8080/doc.html
官网:https://xiaoym.gitee.io/knife4j/documentation/description.html
相关文章:https://juejin.cn/post/7092207390212227086