Swagger---接口文檔維護---簡單使用教程

Swagger可以做什麼

使用Swagger,所有接口的信息,都在代碼裏面了。代碼即接口文檔,接口文檔即代碼,即可以做到代碼的迭代更新時,接口文檔也能更方便及時更新,減少很多維護接口文檔的工作。

Swagger的簡單使用

1、jar的maven引用

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2、配置類

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * @author ljs
 * @data 2020/06/09
 *
 */

@Configuration
@EnableSwagger2
public class Swagger {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo()).
                select().
                apis(RequestHandlerSelectors.basePackage("top.san.mc.base.web")).
                paths(PathSelectors.any()).
                build();
                //掃描 top.san.mc.base.web 下的所有@RequestMapper接口也就是controller層,該路徑根據自己的包而定
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().
                title("前後端分離項目中使用swagger2構建restful api").
                description("在springboot項目中集成swagger2").
                contact("xxxxxx").
                version("1.0").build();
    }
}

3、controller層示例

三個主要註解:

  • @Api :表示該類可以被swagger掃描到。
  • @ApiOperation : 對接口的描述,有values和notes兩個屬性。
  • @ApiImplicitParam : 對接口所接收的參數進行描述。

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;


/**
 * @author ljs
 * @data 2020/06/09
 */
@RestController
@Api
@RequestMapping("/users")
public class UserApi {
    @Autowired
    UserService userService;


    @ApiOperation(value = "根據id獲取用戶", notes = "隨便寫點東西")
    @ApiImplicitParam(name = "id", value = "用戶id", required = true, paramType = "path")
    @GetMapping("/{id}")
    public Response<UserDto> getUserById(@PathVariable String id) {

        return Response.ok(userService.findById(id));
    }


    @ApiOperation(value = "根據名字獲取用戶", notes = "???")
    @ApiImplicitParam(name = "name", value = "用戶名字", required = true, paramType = "path")
    @GetMapping("/{name}/search")

    public Response<UserDto> getByName(@PathVariable String name) {
        return Response.ok(userService.findByName(name));
    }


    @ApiOperation(value = "根據id刪除用戶", notes = "???")
    @ApiImplicitParam(name = "id", value = "用戶id", required = true, paramType = "path")
    @DeleteMapping("/{id}")
    public Response<Void> delete(@PathVariable String id) {
        userService.delete(id);
        return Response.ok();
    }

    @ApiOperation(value = "添加用戶", notes = "???")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用戶對象數據", required = true),
            @ApiImplicitParam(name = "password", value = "用戶對象數據", required = true),
    }
    )
    @PostMapping
    @ResponseBody
    public Response<Void> create(@RequestBody UserInsertRequest userInsertRequest) {
        userService.save(userInsertRequest);
        return Response.ok();
    }

    @ApiOperation(value = "根據id修改用戶", notes = "???")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userModel", value = "用戶對象數據", required = true),
            @ApiImplicitParam(name = "id", value = "用戶id", required = true)
    }
    )
    @PutMapping("/{id}")
    public Response<Void> update(@RequestBody UserUpdataRequest userUpdataRequest, @PathVariable String id) {
        userService.update(id, userUpdataRequest);
        return Response.ok();
    }

    @ApiOperation(value = "分頁測試", notes = "???")
    @ApiImplicitParam(name = "id", value = "用戶id", required = true, paramType = "path")
    @GetMapping
    public Response<Page<UserDto>> getByPage(@PageableDefault(size = 8) Pageable pageable,
                                             UserQueryRequest userQueryRequest) {
        Page<UserDto> userDtoPage = userService.findByPage(pageable, userQueryRequest);
//        //userService.findUser(id);
//            System.out.println(userDtoPage.getTotalElements());
        return Response.ok(userDtoPage);
    }
}

4、效果示例

啓動項目,項目地址加/swagger-ui.html即可訪問swagger提供的web端。
http://127.0.0.1:8080/swagger-ui.html
在這裏插入圖片描述

總結

swagger極大的方便了前後端分離開發的文檔維護,而且swagger使用簡單和提供的界面交互良好,非常棒的工具。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章