SpringBoot 集成 Swagger文檔 生成器

1、當我們還在使用doc 處理我們項目中遇到的前段分離時,後端定義的接口文檔,swagger已經開始流行了,接下來我們就開始用springboot整合
在這裏插入圖片描述
2、文件位置:在這裏插入圖片描述
3、將在pom 加入相關座標:

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

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

4、在新建的實體bean 引入相關組件

package com.carton.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author Liu Runyong
 * @date 2019/12/23
 * @description 用戶信息
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "user", description = "用戶信息")
public class User implements Serializable {

    @ApiModelProperty(value = "用戶主鍵(新增#,更新*)")
    private Integer userId;

    @ApiModelProperty(value = "暱稱")
    private String userName;

    @ApiModelProperty(value = "賬號")
    private String account;

    @ApiModelProperty(value = "賬號")
    private String password;

    @ApiModelProperty(value = "電話")
    private String phone;

    @ApiModelProperty(value = "通勤狀態")
    private String status;

    @ApiModelProperty(value = "是否刪除")
    private Integer isDelete;

    @ApiModelProperty(value = "性別")
    private char sex;

    @ApiModelProperty(value = "年齡")
    private Integer age;

}

5、dao和service 我就不寫了 還是按照我們正常的開發去書寫就行
6、controller

package com.carton.controller;

import com.carton.bean.User;
import com.carton.common.utils.exception.ServerResponse;
import com.carton.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 javax.validation.constraints.Size;
import java.util.List;

/**
 * @author Liu Runyong
 * @date 2019/12/23
 */
@Api(tags = "用戶信息", description = "userController")
@RestController
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("新增用戶信息")
    @PostMapping("insert")
    public ServerResponse insertSelective(@ModelAttribute User user) {
        userService.insertSelective(user);
        return ServerResponse.createBySuccessMessage("success");
    }


    @ApiOperation("驗證用戶是否存在")
    @PostMapping("repeat")
    public ServerResponse repeatUser(String phone) {
        if (userService.repeatUser(phone)) {
            return ServerResponse.createBySuccessMessage("success");
        } else {
            return ServerResponse.createByErrorMessage("error");
        }
    }


    @ApiOperation("查詢用戶信息")
    @GetMapping("select")
    public ServerResponse selectAllByConditions() {
        return ServerResponse.createBySuccess(userService.selectAllByConditions());
    }


    @ApiOperation("編輯用戶信息")
    @PutMapping("update")
    public ServerResponse updateSelectiveByPrimaryKey(@ModelAttribute User user) {
        userService.updateSelectiveByPrimaryKey(user);
        return ServerResponse.createBySuccessMessage("success");
    }


    @ApiOperation("刪除用戶")
    @DeleteMapping("delete")
    public ServerResponse deleteByPrimaryKey(@Size(min = 1, message = "請選擇刪除對象") List<Integer> ids) {
        userService.deleteByPrimaryKey(ids);
        return ServerResponse.createBySuccessMessage("success");
    }

}

7、將swagger加入到springboot啓動項目中

package com.carton;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger 配置
 *
 * @author Liurunyong
 * @date 2019/12/23
 */

@Configuration
@EnableSwagger2
public class Swagger2 {

    /**
     * swagger2的配置文件
     */

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.carton.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 構建 api文檔的詳細信息函數,注意這裏的註解引用的是哪個
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Carton API接口文檔")
                .contact(new Contact("Liurunyong", "http://www.lrycq.cn", "[email protected]"))
                .version("1.0")
                .description("API文檔")
                .build();
    }


}

然後就大功告成了,是不是 還可以
在這裏插入圖片描述

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