新聞中心
這篇文章主要介紹SpringBoot+Swagger-ui如何自動(dòng)生成API文檔,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、磐石網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為磐石等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
什么是Swagger
Swagger是一個(gè)Restful風(fēng)格接口的文檔在線(xiàn)自動(dòng)生成和測(cè)試的框架
官網(wǎng):http://swagger.io
官方描述:The World's Most Popular Framework for APIs.
先看一下 swagger-ui 生成的api 的效果吧
然后我們打開(kāi)查詢(xún)所有用戶(hù)的api 看到api 內(nèi)容
然后在服務(wù)器運(yùn)行的狀態(tài)下點(diǎn)擊 try it out 測(cè)試查詢(xún)功能
接著打開(kāi)新增的api 查看
好了這個(gè)就是自動(dòng)生成的api 效果。接下來(lái)我們就看怎么在我們的項(xiàng)目中使用swagger-ui 吧
springboot 集成 swagger -ui
1、添加依賴(lài)
io.springfox springfox-swagger2 2.2.2 io.springfox springfox-swagger-ui 2.2.2
2、編寫(xiě)配置文件
在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)信息 * 通過(guò)select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來(lái)控制哪些接口暴露給Swagger來(lái)展現(xiàn), * 本例采用指定掃描的包路徑來(lái)定義指定要建立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)在文檔頁(yè)面中) * 訪(fǎng)問(wè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 = "用戶(hù)的增刪改查") public class UserController { @Autowired private UserService userService; /** * 查詢(xún)所有的用戶(hù) * api :localhost:8099/users * @return */ @RequestMapping(method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "獲取用戶(hù)列表,目前沒(méi)有分頁(yè)") public ResponseEntity
注解含義:
@Api:用在類(lèi)上,說(shuō)明該類(lèi)的作用。 @ApiOperation:注解來(lái)給API增加方法說(shuō)明。 @ApiImplicitParams : 用在方法上包含一組參數(shù)說(shuō)明。 @ApiImplicitParam:用來(lái)注解來(lái)給方法入?yún)⒃黾诱f(shuō)明。 @ApiResponses:用于表示一組響應(yīng) @ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息 code:數(shù)字,例如400 message:信息,例如"請(qǐng)求參數(shù)沒(méi)填好" response:拋出異常的類(lèi) @ApiModel:描述一個(gè)Model的信息(一般用在請(qǐng)求參數(shù)無(wú)法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候) @ApiModelProperty:描述一個(gè)model的屬性
注意:@ApiImplicitParam的參數(shù)說(shuō)明:
paramType會(huì)直接影響程序的運(yùn)行期,如果paramType與方法參數(shù)獲取使用的注解不一致,會(huì)直接影響到參數(shù)的接收。
4、啟動(dòng)項(xiàng)目效果圖:
服務(wù)器啟動(dòng)后訪(fǎng)問(wèn) 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è)資訊頻道!
文章題目:SpringBoot+Swagger-ui如何自動(dòng)生成API文檔
新聞來(lái)源:http://ef60e0e.cn/article/jjcjeg.html