SpringBoot集成Swagger-Bootstrap-UI(已改名為Knife4j)


1、前言

在上一篇文章中使用了SpringBoot整合Swagger2,搭建Restful API在線文檔( 鏈接 )。但是有大佬開發出了功能更加強大的在線文檔框架——Swagger-Bootstrap-UI,現在已經更名為Knife4j,因為僅僅一個Swagger-Bootstrap-UI皮膚已經不能滿足開發者的需求,需要增加更多的服務端代碼來滿足開發者的需求,所以更改后的Knife4j不僅僅將前身的Ui皮膚通過Vue技術棧進行了重寫,也增加了更多個性化的特性增強功能,基於springfox項目以及OpenAPI的規范,當然它也是基於Swagger的。Knife4j目前主要支持以Java開發為主,並且是依賴於大環境下使用的Spring MVC、Spring Boot、Spring Cloud框架。當然,Knife4j也提供了離線版本,只要是符合Swagger的OpenAPI版本的規范JSON,都可以通過簡單的配置進行適配,離線版本是適合於任何語言中使用Swagger的,非常的靈活、方便。

  1. 該開源項目GitHub地址:https://github.com/xiaoymin/Swagger-Bootstrap-UI
  2. 該開源項目中文文檔地址(knife4j):https://doc.xiaominfo.com

下面使用SpringBoot來整合Knife4j,如下所示:

2、添加Maven依賴

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

3、創建Config配置類

基於之前例子的代碼:

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.thr.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                // 頁面標題
                .title("標題:springboot整合Knife4j項目API文檔")
                // 項目描述
                .description("描述:springboot整合Knife4j項目API文檔")
                // 服務條款網址
                .termsOfServiceUrl("https://www.cnblogs.com/tanghaorong")
                // 作者信息
                .contact(new Contact("唐浩榮","www.tanghaorong.com","XXXXXX@qq.com"))
                // 許可證
                .license("Apache 3.0")
                // 版本號
                .version("v1.0")
                .build();
    }
}

4、編寫Restful接口

注意:Swagger的注解同樣適用於Knife4j,因為它本來就是在Swagger的基礎上來設計這個框架的。

package com.thr.controller;

import com.thr.entity.User;
import com.thr.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "用戶",tags = "用戶信息模塊")
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查詢所有用戶信息
     * @return
     */
    @GetMapping(value = "/findAll")
    @ApiOperation(value = "查詢所有用戶信息", notes = "獲取所有用戶信息")
    public List<User> findAll(){
        List<User> userList = userService.findAll();
        return userList;
    }
    /**
     * 根據id查詢用戶信息
     * @param id
     * @return
     */
    @GetMapping(value = "/findUserById/{id}")
    @ApiOperation(value = "根據id查詢用戶信息", notes = "根據id獲取用戶信息")
    public User findUserById(@PathVariable(value = "id") Integer id){
        return userService.findUserById(id);
    }

    /**
     * 添加用戶信息
     * @param user
     */
    @PostMapping(value = "/addUser")
    @ApiOperation(value = "添加用戶信息")
    public void addUser(User user){
        user.setUsername("John");
        user.setPassword("123456");
        user.setAge(22);
        user.setAddress("中國重慶");
        userService.addUser(user);
    }
    /**
     * 修改用戶信息
     * @param user
     */
    @PutMapping(value = "/updateUserById")
    @ApiOperation(value = "修改用戶信息")
    public void updateUserById(User user){
        user.setId(3L);
        user.setUsername("Marry");
        user.setPassword("123456");
        user.setAge(20);
        user.setAddress("中國湖南");
        userService.updateUserById(user);
    }
    /**
     * 刪除用戶信息
     * @param id
     */
    @DeleteMapping(value = "/deleteUserById/{id}")
    @ApiOperation(value = "刪除用戶信息")
    public void deleteUserById(@PathVariable(value = "id") Integer id){
        System.out.println("Delete請求...");
        userService.deleteUserById(id);
    }
}

5、啟動項目並測試

啟動項目,如果沒有報錯,然后訪問地址:http://localhost:{自己設置的端口}/doc.html 即可。我的地址為:http://localhost:8080/doc.html#/home   效果圖,如下:

image


測試接口(這里就測試一個查詢所有用戶信息),效果圖如下:

image

可以發現使用Knife4j的調試很像PostMan來測試接口。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM