十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹SpringBoot+Swagger-ui如何自動(dòng)生成API文檔,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、磐石網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為磐石等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
什么是Swagger
Swagger是一個(gè)Restful風(fēng)格接口的文檔在線自動(dòng)生成和測試的框架
官網(wǎng):http://swagger.io
官方描述:The World's Most Popular Framework for APIs.
先看一下 swagger-ui 生成的api 的效果吧
然后我們打開查詢所有用戶的api 看到api 內(nèi)容
然后在服務(wù)器運(yùn)行的狀態(tài)下點(diǎn)擊 try it out 測試查詢功能
接著打開新增的api 查看
好了這個(gè)就是自動(dòng)生成的api 效果。接下來我們就看怎么在我們的項(xiàng)目中使用swagger-ui 吧
springboot 集成 swagger -ui
1、添加依賴
io.springfox springfox-swagger2 2.2.2 io.springfox springfox-swagger-ui 2.2.2
2、編寫配置文件
在application 同級(jí)目錄新建 Swagger2 文件
package com.abel.example; 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; /** * Created by yangyibo on 2018/9/7. */ @Configuration @EnableSwagger2 public class Swagger2 { /** * 創(chuàng)建API應(yīng)用 * apiInfo() 增加API相關(guān)信息 * 通過select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來控制哪些接口暴露給Swagger來展現(xiàn), * 本例采用指定掃描的包路徑來定義指定要建立API的目錄。 * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.abel.example.controller")) .paths(PathSelectors.any()) .build(); } /** * 創(chuàng)建該API的基本信息(這些基本信息會(huì)展現(xiàn)在文檔頁面中) * 訪問地址:http://項(xiàng)目實(shí)際地址/swagger-ui.html * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs") .description("更多請(qǐng)關(guān)注https://blog.csdn.net/u012373815") .termsOfServiceUrl("https://blog.csdn.net/u012373815") .contact("abel") .version("1.0") .build(); } }
3、在controller上添加注解,自動(dòng)生成API
注意:
package com.abel.example.controller; import javax.servlet.http.HttpServletRequest; import java.util.Map; import com.abel.example.bean.User; import io.swagger.annotations.*; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import com.abel.example.service.UserService; import com.abel.example.util.CommonUtil; @Controller @RequestMapping(value = "/users") @Api(value = "用戶的增刪改查") public class UserController { @Autowired private UserService userService; /** * 查詢所有的用戶 * api :localhost:8099/users * @return */ @RequestMapping(method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "獲取用戶列表,目前沒有分頁") public ResponseEntity
注解含義:
@Api:用在類上,說明該類的作用。 @ApiOperation:注解來給API增加方法說明。 @ApiImplicitParams : 用在方法上包含一組參數(shù)說明。 @ApiImplicitParam:用來注解來給方法入?yún)⒃黾诱f明。 @ApiResponses:用于表示一組響應(yīng) @ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息 code:數(shù)字,例如400 message:信息,例如"請(qǐng)求參數(shù)沒填好" response:拋出異常的類 @ApiModel:描述一個(gè)Model的信息(一般用在請(qǐng)求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候) @ApiModelProperty:描述一個(gè)model的屬性
注意:@ApiImplicitParam的參數(shù)說明:
paramType會(huì)直接影響程序的運(yùn)行期,如果paramType與方法參數(shù)獲取使用的注解不一致,會(huì)直接影響到參數(shù)的接收。
4、啟動(dòng)項(xiàng)目效果圖:
服務(wù)器啟動(dòng)后訪問 http://localhost:8099/swagger-ui.html 效果如下
點(diǎn)擊查看效果
以上是“SpringBoot+Swagger-ui如何自動(dòng)生成API文檔”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!