如何在SpringBoot中對(duì)Swagger進(jìn)行配置?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司專注于華亭企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站制作。華亭網(wǎng)站建設(shè)公司,為華亭等地區(qū)提供建站服務(wù)。全流程按需求定制開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)<!--web方便測(cè)試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger2核心包 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui 可視化界面 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
Swagger可視化界面可分為三個(gè)區(qū)域
Swagger相關(guān)配置
package com.example.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import java.util.ArrayList; @Configuration @EnableSwagger2 //開啟Swagger的使用 public class SwaggerConfig { @Bean //Swagger的使用主要是要將docket對(duì)象傳入IOC容器 public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //關(guān)于文檔的各種信息 .enable(true) //使Swagger生效 .groupName("常安祖") .select()//選擇掃描的接口 .apis(RequestHandlerSelectors.basePackage("com.example.controller"))//指定掃描的接口 .build(); } public ApiInfo apiInfo(){ Contact contact = new Contact("長(zhǎng)安","https://blog.csdn.net/weixin_45647685","719801748@qq.com");//個(gè)人的聯(lián)系方式 return new ApiInfo("長(zhǎng)安的文檔", "長(zhǎng)安的開發(fā)文檔", "1.0", "urn:tos",null, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());//文檔的各種信息 } }
@ApiModel( ) //主要用來標(biāo)注返回的實(shí)體類
@ApiModelProperty( ) //主要用來標(biāo)注實(shí)體類中的屬性
案例:
@ApiModel("用戶的實(shí)體類") public class User implements Serializable { @ApiModelProperty("用戶的id") private Integer id; @ApiModelProperty("用戶的姓名") private String name; @ApiModelProperty("用戶的年紀(jì)") private Integer age; public Integer getId() { return id; } public User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
@ApiModelProperty用來標(biāo)注API接口
案例:
package com.yangzihao.controller; import com.yangzihao.entity.User; import io.swagger.annotations.ApiModelProperty; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class UserController { @ApiModelProperty("得到一個(gè)User") @GetMapping("/getUser") public User getUser(){ return new User(1,"測(cè)試",18); } }
關(guān)于如何在SpringBoot中對(duì)Swagger進(jìn)行配置問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
本文標(biāo)題:如何在SpringBoot中對(duì)Swagger進(jìn)行配置-創(chuàng)新互聯(lián)
文章出自:http://www.chinadenli.net/article16/dhsdgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)、定制開發(fā)、商城網(wǎng)站、網(wǎng)站建設(shè)、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容