knife4j ,swagger接口文档升级开源项目!

swagger

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步

但是相对来说swagger的ui页面不是很友好 这时候knife4j 就能够很好的代替他了,废话不多说 springboot 集成knife4j 和springboot 集成swagger 很相似
页面访问: http://localhost:8080/doc.html#/home
pom.xml

   <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.1</version>
    </dependency>

SwaggerConfig配置文件


/**
 * @Author:linjunbo
 * @Description:
 * @Date: 2020/2/26 9:45
 */
@Configuration
@EnableSwagger2
//@EnableSwaggerBootstrapUi
public class SwaggerConfig {
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目名称")
                .description("项目详情")
                .termsOfServiceUrl("http://localhost:8080/")//项目地址
                .contact(new Contact("linjunbo","https://blog.csdn.net/weixin_41930050","[email protected]"))
                .version("1.0")//项目版本
                .build();
    }
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.Controller")) //这里写的是API接口所在的包位置
                .paths(PathSelectors.any())
                .build();
    }

}

实体类

@Data
@NoArgsConstructor//无参构造函数
@AllArgsConstructor//有参构造函数
@ApiModel("用户对象")
public class User {
    @ApiModelProperty(required = true,notes = "ID",example = "1")
    private Integer id;
    @ApiModelProperty(required = true,notes = "名字",example = "林俊杰")
    private String name;

}

控制层

@RestController
@RequestMapping("/test")
@Api(tags = "HELLO CONTROLLER 测试功能接口")
public class TestSwagger {


    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "int",paramType = "path",example = "1")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "接口返回成功状态"),
            @ApiResponse(code = 500, message = "接口返回未知错误,请联系开发人员调试")
    })
        @ApiOperation(value = "通过ID获取用户", notes = "通过用户ID获取用户信息")
        @GetMapping("/getUser/{id}")
        public User findById(@ApiParam(value = "用户ID",required = true) @PathVariable int id){
            return  new User(id,"l林俊波");
        }

        @PostMapping("/userByName")
        @ApiOperation(value = "直接返回ID", notes = "通过用户姓名获取用户信息")
        public String findByName(@ApiParam(value = "用户姓名",required = true,example = "hello") @RequestParam String  name){
            return  name;
        }


}

还能够生成4种离线文件。感觉挺有用的 特别是公司使用的是文档来进行前后端沟通的
效果图
在这里插入图片描述

在这里插入图片描述

发布了78 篇原创文章 · 获赞 5 · 访问量 7361

猜你喜欢

转载自blog.csdn.net/weixin_41930050/article/details/104516334