栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合knife4j文档

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合knife4j文档

1.knife4j快速入门:

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。说白了就是swagger文档的增强版,

特点:修改了展示方式,swagger为上下展示;knife4j为左右展示《更符合中国人的阅读习惯》,测试功能更加详细与简单明了

2.springboot开始整合knife4j文档:

添加pox.xm依赖;


		
			com.github.xiaoymin
			knife4j-spring-boot-starter
			3.0.3
		

编写Knife4jConfiguration配置类;

@Configuration
@EnableSwagger2
public class Knife4jConfiguration {
	@Bean(value = "defaultApi2")
	
	public Docket defaultApi2() {
		Docket docket=new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(new ApiInfoBuilder()
						.title("测试==demo")
						.description("# swagger-bootstrap-ui-demo RESTful APIs")
						.termsOfServiceUrl("http://www.xx.com/")
						.contact(new Contact("玉面小白龙","","123@qq.com"))
						.version("1.0")
						.build())
				//分组名称
				.groupName("2.X版本")
				.select()
              //这里指定文档Controller层扫描包路径 
				.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
				.paths(PathSelectors.any())
				.build();
		return docket;
	}
}

最后注解的实现

controller层: 使用的位置

@Api(tags = "学生模块")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentMapper studentMapper;

      
    @ApiOperationSupport(author = "杨涛涛")
    @ApiOperation(value ="根据学号查询")
    @ApiImplicitParam(name = "studentid",value = "学生学号",readOnly = true,dataTypeClass = Integer.class)
    @GetMapping("getById/{studentid}")
    public R getById(@PathVariable("studentid") Integer studentid){
         Student byId = studentService.getById(studentid);
        return R.ok().data("byId",byId);
    }

}

访问路径 :http://localhost:8080/doc.html

更加直观的测试方式:

注意事项:

使用knife4j文档切记方法上不要是用@RequestMapping注解来交互数据;不然展示的效果入图:一个方法会帮你全部的增删查改方法全部展现出来;实际开发中 一个接口只能对应一个测试方法,查询就用@GetMapping 增加就用@PostMapping 修改就用@PutMapping  删除就用@deleteMapping

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号