spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的代碼詳解
java代碼
package com.oauth.util;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;
@Configuration
@EnableSwagger2
//是否開啟swagger
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class Swagger2 {
// swagger2的配置文件,這里可以配置swagger2的一些基本的內(nèi)容,比如掃描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 為當(dāng)前包路徑
.apis(RequestHandlerSelectors.basePackage("com.oauth.controller")).paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 頁(yè)面標(biāo)題
.title("Swagger2")
// 創(chuàng)建人信息
.contact(new Contact("scy", "666", "888"))
// 版本號(hào)
.version("1.0")
// 描述
.description("API 描述").build();
}
}
yml文件
server: port: 8587 spring: application: name: auth eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8090/eureka/ swagger: enable: true
swagger:
enable: true 這里是設(shè)置是否啟動(dòng) 本地和測(cè)試環(huán)境為true 正式環(huán)境為false
controller
package com.oauth.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("api")
@Api(value = "測(cè)試接口", tags = "IndexController")
public class IndexController {
@ApiOperation(value = "hello")
@GetMapping("hello")
public String hello() {
return "Hello World";
}
@ApiOperation(value = "hello2")
@GetMapping("api/hello")
public String apiHello() {
return "Hello World";
}
}
打開swagger頁(yè)面 localhost:端口號(hào)/swagger-ui.html

如果swagger:
enable: false 這里設(shè)置為false

總結(jié)
到此這篇關(guān)于spring boot-2.1.16整合swagger-2.9.2 含yml配置文件的文章就介紹到這了,更多相關(guān)spring boot整合swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合swagger3和knife4j的詳細(xì)過(guò)程
- Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
- Spring?Boot?2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問(wèn)題的完美解決辦法
- 教你怎么用SpringBoot整合Swagger作為API
- 關(guān)于springboot整合swagger問(wèn)題及解決方法
- SpringBoot整合Swagger3生成接口文檔過(guò)程解析
- Spring Boot整合swagger使用教程詳解
- Swagger3.0 整合spring boot2.7x避免swagger2.0與boot2.7沖突問(wèn)題
相關(guān)文章
springboot前端傳參date類型后臺(tái)處理的方式
這篇文章主要介紹了springboot前端傳參date類型后臺(tái)處理的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
java 中Comparable與Comparator詳解與比較
這篇文章主要介紹了java 中Comparable與Comparator詳解與比較的相關(guān)資料,需要的朋友可以參考下2017-04-04
關(guān)于post請(qǐng)求內(nèi)容無(wú)法重復(fù)獲取的解決方法
這篇文章主要介紹了關(guān)于post請(qǐng)求內(nèi)容無(wú)法重復(fù)獲取的解決方法,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
java 利用反射機(jī)制,獲取實(shí)體所有屬性和方法,并對(duì)屬性賦值
這篇文章主要介紹了 java 利用反射機(jī)制,獲取實(shí)體所有屬性和方法,并對(duì)屬性賦值的相關(guān)資料,需要的朋友可以參考下2017-01-01
使用Jmeter進(jìn)行http接口測(cè)試的實(shí)踐
本文主要針對(duì)http接口進(jìn)行測(cè)試,使用Jmeter工具實(shí)現(xiàn)。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
使用mybatis框架連接mysql數(shù)據(jù)庫(kù)的超詳細(xì)步驟
MyBatis是目前java項(xiàng)目連接數(shù)據(jù)庫(kù)的最流行的orm框架了,下面這篇文章主要給大家介紹了關(guān)于使用mybatis框架連接mysql數(shù)據(jù)庫(kù)的超詳細(xì)步驟,文中通過(guò)實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
java多態(tài)實(shí)現(xiàn)電子寵物系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java多態(tài)實(shí)現(xiàn)電子寵物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

