欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

教你利用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è)務(wù)邏輯異常").build());
        responseMessageList.add(new ResponseMessageBuilder().code(400).message("網(wǎng)絡(luò)協(xié)議錯誤").build());
        responseMessageList.add(new ResponseMessageBuilder().code(500).message("服務(wù)器內(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")//接口文檔標題設(shè)置
                .description("API接口列表")//描述
                .version("1.0")//版本號
                .build();
    }
}

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

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

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

(3)入?yún)⑿揎?。使用@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 = "服務(wù)錯誤")
    })
    @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)出每個字段對應(yīng)的說明。最后還需要在實體類中定義一層。使用@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未恢復(fù)  2已恢復(fù)
	 */
	@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;
}

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

相關(guān)文章

  • SpringCloud的Hystrix簡單介紹

    SpringCloud的Hystrix簡單介紹

    這篇文章主要介紹了SpringCloud的Hystrix簡單介紹,SpringCloud Hystrix是Netflix開源的一款容錯框架,具備服務(wù)降級,服務(wù)熔斷,依賴隔離,監(jiān)控(Hystrix Dashboard)等功能,同樣具有自我保護能力,需要的朋友可以參考下
    2023-07-07
  • Java之MyBatis入門詳解

    Java之MyBatis入門詳解

    這篇文章主要介紹了Java之MyBatis入門詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java編程中使用throw關(guān)鍵字拋出異常的用法簡介

    Java編程中使用throw關(guān)鍵字拋出異常的用法簡介

    這篇文章主要介紹了Java編程中使用throw關(guān)鍵字拋出異常的用法,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • 使用Java編寫GUI對話框的教程

    使用Java編寫GUI對話框的教程

    這篇文章主要介紹了使用Java編寫GUI對話框的教程,是Java圖形化編程中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • java對接支付寶支付項目的實戰(zhàn)記錄

    java對接支付寶支付項目的實戰(zhàn)記錄

    最近公司有一個需求是接入第三方支付(微信&支付寶),我接到了支付寶支付,所以下面這篇文章主要給大家介紹了關(guān)于java對接支付寶支付項目的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Mybatis generator的使用全面解析

    Mybatis generator的使用全面解析

    這篇文章主要介紹了Mybatis generator的使用,非常不錯,具有參考借鑒價值,對mybatis generator的使用相關(guān)知識感興趣的朋友一起學習吧
    2016-09-09
  • Java中的原生post請求方式

    Java中的原生post請求方式

    這篇文章主要介紹了Java中的原生post請求方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Java的HashSet源碼詳解

    Java的HashSet源碼詳解

    這篇文章主要介紹了Java的HashSet源碼詳解,HashSet底層封裝的是HashMap,所以元素添加會放到HashMap的key中,value值使用new Object對象作為value,所以HashSet和HashMap的所具有的特點是類似的,需要的朋友可以參考下
    2023-09-09
  • java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實現(xiàn)

    java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實現(xiàn)

    這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)單鏈表的實現(xiàn)的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • mybatis中${}和#{}取值的區(qū)別分析

    mybatis中${}和#{}取值的區(qū)別分析

    mybatis中使用sqlMap進行sql查詢時,經(jīng)常需要動態(tài)傳遞參數(shù),在動態(tài)SQL解析階段, #{ } 和 ${ } 會有不同的表現(xiàn),這篇文章主要給大家介紹了關(guān)于mybatis中${}和#{}取值區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2021-09-09

最新評論