springboot整合knife4j文档

1.knife4j快速入门:

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

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

2.springboot开始整合knife4j文档:

添加pox.xm依赖;

<!-- knife4j依赖  -->
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>3.0.3</version>
		</dependency>

编写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;

      /**
     * 根据学号查询数据
     * @param studentid
     * @return
     */
    @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


                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值