swagger 和 knife4j
1.导入依赖
1 2 3 4 5 6 | io.springfox springfox-swagger2 io.springfox springfox-swagger-ui |
2.添加配置类(注意:如果配置类不在一个模块下,那么要进行包扫描@ComponentScan(" "))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @Configuration @EnableSwagger2 // 开启Swagger功能 public class SwaggerConfiguration { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() // 要扫描的API(Controller)基础包 .apis(RequestHandlerSelectors.basePackage("com.heima")) .paths(PathSelectors.any()) .build(); } /** * 构建Api信息 * @return */ private ApiInfo buildApiInfo() { Contact contact = new Contact("黑马程序员","",""); return new ApiInfoBuilder() .title("黑马头条-平台管理API文档") .description("平台管理服务api") .contact(contact) .version("1.0.0").build(); } } |
3.controllor标注注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @RestController @RequestMapping("/api/v1/channel") @Api(tags = "频道管理API") public class WmChannelController { // 注入服务接口 @Autowired private IWmChannelService wMChannelService; /** * 根据名称模糊查询分页列表 * * @param dto * @return */ @PostMapping("/list") @ApiOperation(value = "根据名称模糊查询分页列表", notes = "author:syl") // value指名称 notes 备注 @ApiImplicitParam(name = "dto", value = "查询对象", required = true, dataType = "ChannelDto") public ResponseResult listByName(@RequestBody ChannelDto dto) { return wMChannelService.listByName(dto); } } |
4.实体类标注注解
@Data @EqualsAndHashCode(callSuper = true) public class ChannelDto extends PageRequestDto { /** * 频道名称 */ @ApiModelProperty(value = "频道名称") private String name; }
5.访问地址:http://localhost:9001/swagger-ui.html
使用knife4j
1.导入依赖
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> </dependency>
2.添加配置类(注意:1.注释掉swagger配置 2.如果配置类不在一个模块下,那么要进行包扫描@ComponentScan(" "))
@Configuration @EnableSwagger2 @EnableKnife4j @Import(BeanValidatorPluginsConfiguration.class) public class MyKnife4jConfiguration { @Bean(value = "defaultApi2") public Docket defaultApi2() { Docket docket=new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //这里指定Controller扫描包路径 .apis(RequestHandlerSelectors.basePackage("com.heima")) .paths(PathSelectors.any()) .build(); return docket; } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("黑马头条API文档") .description("黑马头条API文档") .version("1.0") .build(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架