最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

教你利用springboot集成swagger并生成接口文檔

 更新時間:2021年05月26日 15:06:52   作者:185的阿平  
有很多小伙伴不會利用springboot集成swagger并生成接口文檔,今天特地整理了這篇文章,文中有非常詳細的代碼圖文介紹及代碼示例,對不會這個方法的小伙伴們很有幫助,需要的朋友可以參考下

效果圖

實現(xiàn)步驟

1.maven中引入jar包,不同版本的swagger可能頁面效果不一樣。

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

2.啟動類加上@EnableSwagger2注解,并在同路徑下創(chuàng)建全局配置類。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
import java.util.ArrayList;
import java.util.List;
 
@Configuration
public class Swagger2Config {
 
 
    @Bean
    public Docket createRestApi() {
        //這塊是定義全局返回碼
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到資源").build());
        responseMessageList.add(new ResponseMessageBuilder().code(409).message("業(yè)務邏輯異常").build());
        responseMessageList.add(new ResponseMessageBuilder().code(400).message("網(wǎng)絡協(xié)議錯誤").build());
        responseMessageList.add(new ResponseMessageBuilder().code(500).message("服務器內(nèi)部錯誤--->Exception").build());
        responseMessageList.add(new ResponseMessageBuilder().code(502).message("nginx異常").build());
        return new Docket(DocumentationType.SWAGGER_2).globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("order.controller"))//包的根路徑
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("ORDER-PROVIDER")//接口文檔標題設置
                .description("API接口列表")//描述
                .version("1.0")//版本號
                .build();
    }
}

3.在相應的controller層寫對應注解。一般來說接口文檔關注的幾點,網(wǎng)絡協(xié)議,接口入?yún)ⅲ涌诔鰠?,方法類型。拿一個conteller舉例。分幾步:

(1)對整個controller類加上@Api注解標識為一個swagger管理的類。

(2)方法修飾,加上@ApiOperation對方法進行說明。主要參數(shù)如圖

(3)入?yún)⑿揎棥J褂聾ApiImplicitParams和@ApiImplicitParam一起封裝。如果入?yún)?shù)含有實體對象,其中@ApiImplicitParam的name屬性就定義為類的類型,這樣才能展示。如果為基本類型,name即為屬性名稱。

 (4)出參修飾??梢允褂聾ApiResponses和@ApiResponse一起封裝。如果返回中含有泛型實體(目前來說接口返回都是一個基本返回對象包裝實例數(shù)據(jù)對象)。此時想要展現(xiàn)出來,就需要在接口方法處指定返回的泛型,然后在@ApiResponse注解中不要使用Response屬性指定返回類型。

@RestController
@RequestMapping("/manager/blacklist")
@Api("黑名單管理")
public class BlackListController{
    @Autowired
    private BlackListService blackListService;
 
    /**
     * 分頁列表
     */
    @ApiOperation(value = "list",notes = "分頁列表查詢",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "BlackListDto",value = "黑名單實體",required = true)
            @ApiImplicitParam(paramType = "query",name = "tokenId",value = "鑒權(quán)Id",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功" ),
            @ApiResponse(code= 500,message = "服務錯誤")
    })
    @PostMapping("/list")
    public ResponeModel<BlackListDto> list(@RequestBody BlackListDto blackListDto,String tokenId){
        PageInfo<BlackListDto> list = blackListService.pageList(blackListDto);
        return ResponeModel.ok(list);
    }
}

(5)實體修飾。要想在swagger界面展現(xiàn)出每個字段對應的說明。最后還需要在實體類中定義一層。使用@ApiModel定義類和@ApiModelProperty定義屬性。

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
 
import java.util.Date;
 
/**
 * 黑名單表
 * 
 * @email ${email}
 * @date 2021-05-13 14:24:39
 */
@Data
@ApiModel(value = "BlackListDto",description = "黑名單實體信息")
public class BlackListDto  {
	private static final long serialVersionUID = 1L;
 
	/**
	 * 主鍵id
	 */
	@ApiModelProperty(value = "主鍵ID",name = "id")
	private Long id;
	/**
	 * 買家編號
	 */
	@ApiModelProperty(value = "買家編號",name = "buyerNo")
	private String buyerNo;
	/**
	 * 備注,進入黑名單的原因
	 */
	@ApiModelProperty(value = "備注",name = "remarks")
	private String remarks;
	/**
	 * 黑名單狀態(tài):1未恢復  2已恢復
	 */
	@ApiModelProperty(value = "黑名單狀態(tài)",dataType = "Integer")
	private Integer status;
	/**
	 * 字符型保留字段1.
	 */
	@ApiModelProperty(value = "字符型保留字段1",name = "fldS1")
	private String fldS1;
	/**
	 * 字符型保留字段2.
	 */
	@ApiModelProperty(value = "字符型保留字段2",name = "fldS2")
	private String fldS2;
	/**
	 * 字符型保留字段3.
	 */
	@ApiModelProperty(value = "字符型保留字段3",name = "fldS3")
	private String fldS3;
	/**
	 * 數(shù)值型保留字段
	 */
	@ApiModelProperty(value = "數(shù)值型保留字段1",name = "fldN1")
	private Integer fldN1;
	/**
	 * 數(shù)值型保留字段
	 */
	@ApiModelProperty(value = "數(shù)值型保留字段2",name = "fldN2")
	private Integer fldN2;
	/**
	 * 創(chuàng)建時間
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",locale = "zh")
	@ApiModelProperty(value = "創(chuàng)建時間",name = "createDate")
	private Date createDate;
	/**
	 * 修改人
	 */
	@ApiModelProperty(value = "修改人",name = "updateBy")
	private String updateBy;
	/**
	 * 修改時間
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",locale = "zh")
	@ApiModelProperty(value = "修改時間",name = "updateDate")
	private Date updateDate;
	/**
	 * 創(chuàng)建人ID
	 */
	@ApiModelProperty(value = "創(chuàng)建人",name = "createBy")
	private String createBy;
}

到此這篇關于教你利用springboot集成swagger并生成接口文檔的文章就介紹到這了,更多相關springboot集成swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現(xiàn)自動獲取法定節(jié)假日詳細代碼

    Java實現(xiàn)自動獲取法定節(jié)假日詳細代碼

    這篇文章主要給大家介紹了關于Java實現(xiàn)自動獲取法定節(jié)假日的相關資料,獲取并處理節(jié)假日數(shù)據(jù)是一個常見需求,特別是在需要安排任務調(diào)度、假期通知等功能的場景中,需要的朋友可以參考下
    2024-05-05
  • Spring @Value注解失效問題解決方案

    Spring @Value注解失效問題解決方案

    這篇文章主要介紹了Spring @Value注解失效問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 如何解決zookeeper集群重啟Error contacting service.It is probably not running問題

    如何解決zookeeper集群重啟Error contacting service.It 

    本文詳細介紹了Zookeeper集群啟動異常的排查步驟,包括網(wǎng)絡問題、防火墻配置、Java環(huán)境、端口占用、網(wǎng)卡問題、網(wǎng)絡問題以及節(jié)點配置信息的檢查,通過逐一排查和解決這些問題,可以有效地啟動Zookeeper集群
    2024-12-12
  • Java中的按值傳遞和按引用傳遞的代碼詳解

    Java中的按值傳遞和按引用傳遞的代碼詳解

    本文通過實例代碼給大家介紹了Java中的按值傳遞和按引用傳遞的相關知識,感興趣的朋友跟隨腳本之家小編一起學習吧
    2018-06-06
  • 簡單學習Java API 設計實踐

    簡單學習Java API 設計實踐

    API(Application Programming Interface,應用程序編程接口)是一些預先定義的函數(shù),目的是提供應用程序與開發(fā)人員基于某軟件或硬件的以訪問一組例程的能力,而又無需訪問源碼,或理解內(nèi)部工作機制的細節(jié)。需要的可以了解一下
    2019-06-06
  • SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼

    SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼

    Feign是一個開源的Java HTTP客戶端,可以幫助我們在SpringBoot應用中快速構(gòu)建和使用HTTP客戶端,方便實現(xiàn)服務間的通信,本文就來介紹一下SpringBoot使用Feign進行服務間通信的實現(xiàn)示例代碼,感興趣的可以了解一下
    2024-01-01
  • JavaWeb實現(xiàn)打印功能

    JavaWeb實現(xiàn)打印功能

    這篇文章主要介紹了JavaWeb實現(xiàn)打印功能的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • spring boot加載第三方jar包的配置文件的方法

    spring boot加載第三方jar包的配置文件的方法

    本篇文章主要介紹了spring boot加載第三方jar包的配置文件的方法,詳細的介紹了spring boot jar包配置文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • SpringBoot實現(xiàn)Mysql使用MD5進行密碼加密的示例

    SpringBoot實現(xiàn)Mysql使用MD5進行密碼加密的示例

    這篇文章主要介紹了SpringBoot實現(xiàn)Mysql使用MD5進行密碼加密的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • SpringCloud Zuul過濾器和谷歌Gauva實現(xiàn)限流

    SpringCloud Zuul過濾器和谷歌Gauva實現(xiàn)限流

    這篇文章主要介紹了SpringCloud Zuul過濾器和谷歌Gauva實現(xiàn)限流,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03

最新評論

高邑县| 湖北省| 天全县| 贵定县| 宝兴县| 南充市| 南丰县| 侯马市| 霍邱县| 萨嘎县| 新昌县| 灌南县| 郧西县| 雅江县| 克什克腾旗| 扎鲁特旗| 全南县| 南岸区| 叶城县| 金平| 阿图什市| 固镇县| 周口市| 淄博市| 百色市| 外汇| 清涧县| 阿拉善盟| 林西县| 维西| 元阳县| 黔江区| 永春县| 长乐市| 正阳县| 太原市| 会泽县| 襄樊市| 徐水县| 定陶县| 延边|