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
@EnableSwagger2
public 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", //作者
"",
"")) //聯(lián)系人
.description("SpringBootDemo的項目接口API")//詳細信息
.version("1.0.0")//文檔版本號
.termsOfServiceUrl("")//網(wǎng)站地址
.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 = "根據(jù)用戶名獲取用戶信息接口")
@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()用于方法,參數(shù),字段說明;
表示對參數(shù)的添加元數(shù)據(jù)(說明或是否必填等)
name–參數(shù)名
value–參數(shù)說明
required–是否必填
@ApiParam(name="username",value = "用戶名",required = true) String username
效果:

- @ApiModel()用于類
- 表示對類進行說明,用于參數(shù)用實體類接收
- @ApiModelProperty()用于方法,字段
- 表示對model屬性的說明或者數(shù)據(jù)操作更改
- @ApiIgnore()用于類,方法,方法參數(shù)
- 表示這個方法或者類被忽略
- @ApiImplicitParam() 用于方法
- 表示單獨的請求參數(shù)
- @ApiImplicitParams() 用于方法,包含多個@ApiImplicitParam
到此這篇關于SpringBoot集成Swagger2構建在線API文檔的文章就介紹到這了,更多相關SpringBoot集成Swagger2內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Spring MVC利用Swagger2如何構建動態(tài)RESTful API詳解
- 詳解spring cloud整合Swagger2構建RESTful服務的APIs
- SpringBoot2.1 RESTful API項目腳手架(種子)項目
- SpringBoot結合Swagger2自動生成api文檔的方法
- swagger2隱藏在API文檔顯示某些參數(shù)的操作
- SpringBoot2.7?WebSecurityConfigurerAdapter類過期配置
- springboot2+es7使用RestHighLevelClient的示例代碼
- 淺談Springboot2.0防止XSS攻擊的幾種方式
- Spring Boot2配置Swagger2生成API接口文檔詳情
相關文章
Java8中Stream流求最大值最小值的實現(xiàn)示例
本文主要介紹了Java8中Stream流求最大值最小值的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
SpringBoot使用@Validated處理校驗的方法步驟
@Validated?注解的主要目的是啟用和利用?Spring?的驗證框架,它可以用于類上也可以用于方法參數(shù)上,本文給大家介紹了SpringBoot使用@Validated優(yōu)雅的處理校驗的方法步驟,通過代碼示例介紹的非常詳細,需要的朋友可以參考下2024-08-08
Spring?Cloud?Hystrix?服務降級限流策略詳解
這篇文章主要為大家介紹了Spring?Cloud?Hystrix?服務降級限流策略詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Javaweb會話跟蹤技術Cookie和Session的具體使用
本文主要介紹了Javaweb會話跟蹤技術Cookie&Session的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
Spring獲取當前類在容器中的beanname實現(xiàn)思路
這篇文章主要介紹了Spring獲取當前類在容器中的beanname,實現(xiàn)思路只需繼承BeanNameAware接口,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

