SpringBoot集成Swagger2構建在線API文檔的代碼詳解
第一部分:代碼集成
pom.xml
<!--swagger2配置--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.6</version> </dependency>
swagger2配置類
package com.liud.demo.config;import io.swagger.annotations.ApiOperation;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.service.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;/** * TODO * swagger2配置類 * @author liud * @version 1.0 */@Configuration@EnableSwagger2public class Swagger2 { //配置swagger2核心配置 @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) //指定api類型位swagger2.apiInfo(apiInfo()) //用于定義api文檔匯總信息.select()//.apis(RequestHandlerSelectors.basePackage('com.liud.demo.controller')) //指定生成文檔的controller//.apis(RequestHandlerSelectors.any()) //為任何接口生成API文檔//.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //為有@Api注解的Controller生成API文檔.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //為有@ApiOperation注解的方法生成API文檔.paths(PathSelectors.any()).build(); } //api基本信息 private ApiInfo apiInfo(){ return new ApiInfoBuilder().title('SpringBootDemo的項目接口API') //文檔標題.contact(new Contact('liud', //作者 '', '')) //聯系人.description('SpringBootDemo的項目接口API')//詳細信息.version('1.0.0')//文檔版本號.termsOfServiceUrl('')//網站地址.build(); }}
Controller
package com.liud.demo.controller;import com.liud.demo.service.HelloService;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;/** * TODO * * @author liud * @version 1.0 */@RestController@Api(tags = {'hello操作接口'})public class HelloController { @ApiOperation(value = '根據用戶名獲取用戶信息接口') @RequestMapping(value = '/getuserinfo',method = RequestMethod.POST) public String getUserInfo(HttpServletRequest request, @ApiParam(name='username',value = '用戶名',required = true) String username){ return '輸入的姓名:'+username+',這個用戶的信息已經存在!'; }}
第二部分 使用 ①原路徑模式
在瀏覽器上輸入url:http://{ip}:{port}/swagger-ui.html#/
我的地址:http://127.0.0.1:8081/swagger-ui.html
②文檔模式
在瀏覽器上輸入url:http://{ip}:{port}/doc.html
我的地址:http://127.0.0.1:8081/doc.html
第三部分 swagger2常用注解
常用注解:
@Api()用于類;表示標識這個類是swagger的資源tags?表示說明value?也是說明,可以使用tags替代但是tags如果有多個值,會生成多個list
效果:
@ApiOperation()用于方法;表示一個http請求的操作value用于方法描述notes用于提示內容tags可以重新分組(視情況而用)
@ApiParam()用于方法,參數,字段說明;表示對參數的添加元數據(說明或是否必填等)name?參數名value?參數說明required?是否必填
@ApiParam(name='username',value = '用戶名',required = true) String username
效果:
到此這篇關于SpringBoot集成Swagger2構建在線API文檔的文章就介紹到這了,更多相關SpringBoot集成Swagger2內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
