誰(shuí)家面試往死里問(wèn) Swagger ???
前言
說(shuō)個(gè)挺奇葩的事,有個(gè)老鐵給我發(fā)私信吐槽了一下它的面試經(jīng)歷,他去了個(gè)國(guó)企單位面試,然后面試官跟他就Swagger的問(wèn)題聊了半個(gè)多小時(shí)。額~ 面試嘛這些都不稀奇,總能遇到是千奇百怪的人,千奇百怪的問(wèn)題。不過(guò),我分析這個(gè)面試官是不太好意思直接讓他走,哈哈哈!
圖片
什么是Swagger?
Swagger目前是比較主流的RESTful風(fēng)格的API文檔工具,做過(guò)開(kāi)發(fā)的人應(yīng)該都用過(guò)它吧!
圖片
它提供了一套工具和規(guī)范,讓開(kāi)發(fā)人員能夠更輕松地創(chuàng)建和維護(hù)可讀性強(qiáng)、易于使用和交互的API文檔(官方口吻)。
title: Swagger
desc: Swagger 官方網(wǎng)站
logo: https://static1.smartbear.co/swagger/media/assets/images/swagger_logo.svg
link: https://swagger.io/
為什么用Swagger?
以往在沒(méi)有這樣的API文檔工具,開(kāi)發(fā)人員需要手動(dòng)編寫(xiě)和維護(hù)功能API的文檔。而且,由于API變更往往難以及時(shí)更新到文檔中,這可能會(huì)給依賴文檔的開(kāi)發(fā)者帶來(lái)困惑。
說(shuō)幾個(gè)Swagger的特點(diǎn):
- 最重要的一點(diǎn)可以根據(jù)代碼注解自動(dòng)生成API文檔,能生成的絕對(duì)不手寫(xiě),而且API文檔與API定義會(huì)同步更新。
- 它提供了一個(gè)可執(zhí)行的Web界面,支持API在線測(cè)試,可以直接在界面上直接設(shè)置參數(shù)測(cè)試,不用額外的測(cè)試工具或插件。
- 支持多種編程語(yǔ)言,Java、PHP、Python等語(yǔ)言都支持,喜歡什么語(yǔ)言構(gòu)建API都行。
總的來(lái)說(shuō),Swagger可以讓我們更多時(shí)間在專注于編寫(xiě)代碼(摸魚(yú)),而不是花費(fèi)額外精力來(lái)維護(hù)文檔,實(shí)踐出真知先跑個(gè)demo試試。
Swagger搭建
maven 依賴
目前使用的版本是Swagger3.0、Springboot 2.7.6,Swagger2.0與3.0依賴包名稱的變化有些大,需要特別注意一下。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置類
首先我們創(chuàng)建一個(gè)控制器TestController類,里邊只有一個(gè)最簡(jiǎn)單的請(qǐng)求 /test。
@RestController
public class TestController {
@RequestMapping("/test")
public String test(String name) {
return name;
}
}
接下來(lái)創(chuàng)建配置類SwaggerConfig,類標(biāo)注@EnableSwagger2注解是關(guān)鍵,到這最簡(jiǎn)單的Swagger文檔環(huán)境就搭建好了。
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
啟動(dòng)報(bào)錯(cuò)
啟動(dòng)時(shí)可能會(huì)報(bào)如下的錯(cuò)誤,這是由于高版本的Springboot與Swagger版本使用的路徑匹配策略沖突導(dǎo)致的。
Springfox使用的路徑匹配規(guī)則為AntPathMatcher 的,而SpringBoot2.7.6使用的是PathPatternMatcher,兩者沖突了。
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.24.jar:5.3.24]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_341]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.24.jar:5.3.24]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.6.jar:2.7.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.6.jar:2.7.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.6.jar:2.7.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.6.jar:2.7.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.6.jar:2.7.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.6.jar:2.7.6]
at com.springboot101.SwaggerApplication.main(SwaggerApplication.java:10) [classes/:na]
解決方案
這個(gè)錯(cuò)誤的解決辦法比較多,我整理了四種解決此問(wèn)題的方案,你看哪個(gè)更合適你。
1、降低版本
SpringBoot版本降低到2.5.X 、springfox降到3.X 以下可以解決問(wèn)題,不過(guò)不推薦這么做,畢竟降配置做兼容顯得有點(diǎn)傻。
2、統(tǒng)一路徑匹配策略
將SpringMVC的匹配URL路徑的策略改為ant_path_matcher,application.yml文件增加如下的配置:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
3、@EnableWebMvc注解
在配置類SwaggerConfig上標(biāo)注@EnableWebMvc注解也可以解決。
Swagger框架需要通過(guò)解析和掃描帶有注解的Controller類和方法來(lái)生成API文檔。@EnableWebMvc注解會(huì)注冊(cè)一個(gè)RequestMappingHandlerMapping的Bean,并將其作為默認(rèn)的請(qǐng)求映射處理器,以確保這些Controller類和方法能夠被正確處理,可以與Swagger的路徑配置規(guī)則相匹配,從而使得Swagger能夠成功生成API文檔。
@EnableWebMvc
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
4、注冊(cè) BeanPostProcessor
也可以自行實(shí)現(xiàn)兼容邏輯來(lái)解決這個(gè)問(wèn)題,我們可以在Spring容器中注冊(cè)一個(gè)BeanPostProcessor,在該處理器中對(duì) HandlerMappings 進(jìn)行定制。
通過(guò)過(guò)濾掉已存在PatternParser的映射,意味著我們可以將Swagger特定的HandlerMappings添加到HandlerMappings列表中,從而使用自定義的設(shè)置來(lái)替代原有的HandlerMappings。
這樣修復(fù)了可能導(dǎo)致Swagger無(wú)法正常使用的問(wèn)題。
@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
log.warn("修改WebMvcRequestHandlerProvider的屬性:handlerMappings出錯(cuò),可能導(dǎo)致swagger不可用", e);
throw new IllegalStateException(e);
}
}
};
}
訪問(wèn) swagger-ui
到這,問(wèn)題解決!我們?cè)L問(wèn)Swagger文檔路徑 http://127.0.0.1:9002/swagger-ui/index.html ,能夠看到我們寫(xiě)的 API 信息以及一些Swagger 文檔的默認(rèn)配置信息。
圖片
注意到我們只寫(xiě)了一個(gè) /test接口,但這里確把這個(gè)方法的所有請(qǐng)求方式都列了出來(lái),因?yàn)槲覀冊(cè)?nbsp;controller 方法中使用了@RequestMapping注解,并沒(méi)有具體的指定接口的請(qǐng)求方式,所以避免文檔冗余,盡量指定請(qǐng)求方式或者使用指定請(qǐng)求方式的 @XXXMapping 注解。
指定請(qǐng)求方式后:
圖片
API文檔配置
上邊我們?cè)L問(wèn)的文檔中展示的數(shù)據(jù)都是默認(rèn)的配置,現(xiàn)在咱們來(lái)定制化一下文檔。
Springfox提供了一個(gè)Docket對(duì)象,供我們靈活的配置Swagger的各項(xiàng)屬性。Docket對(duì)象內(nèi)提供了很多的方法來(lái)配置文檔,下邊介紹下常用的配置項(xiàng)。
select
select()返回一個(gè)ApiSelectorBuilder對(duì)象,是使用apis()、paths()兩個(gè)方法的前提,用于指定Swagger要掃描的接口和路徑。
apis
默認(rèn)情況下,Swagger會(huì)掃描整個(gè)項(xiàng)目中的接口,通過(guò) apis()方法,你可以傳入一個(gè)RequestHandlerSelector對(duì)象實(shí)例來(lái)指定要包含的接口所在的包路徑。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.springboot101.controller"))
.build();
}
paths
僅將某些特定請(qǐng)求路徑的API展示在Swagger文檔中,例如路徑中包含/test。可以使用 apis() 和 paths()方法一起來(lái)過(guò)濾接口。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/test/**"))
.build();
}
groupName
為生成的Swagger文檔指定分組的名稱,用來(lái)區(qū)分不同的文檔組。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("用戶分組")
.build();
}
圖片
實(shí)現(xiàn)文檔的多個(gè)分組,則需創(chuàng)建多個(gè) Docket 實(shí)例,設(shè)置不同的組名,和組內(nèi)過(guò)濾 API 的條件。
@Bean
public Docket docket1(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("商家分組")
.select()
.paths(PathSelectors.ant("/test1/**"))
.build();
}
圖片
apiInfo
設(shè)置API文檔的基本信息,例如標(biāo)題、描述、版本等。你可以使用ApiInfo對(duì)象自定義信息。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()); // 文檔基礎(chǔ)配置
}
private ApiInfo apiInfo() {
Contact contact = new Contact("小富", "http://fire100.top", "email@example.com");
return new ApiInfoBuilder()
.title("Swagger學(xué)習(xí)")
.description("程序員小富-帶你一起學(xué)習(xí) Swagger")
.version("v1.0.1")
.termsOfServiceUrl("http://fire100.top")
.contact(contact)
.license("許可證")
.licenseUrl("許可鏈接")
.extensions(Arrays.asList(
new StringVendorExtension("我是", "小富"),
new StringVendorExtension("你是", "誰(shuí)")
))
.build();
}
對(duì)應(yīng)的Swagger文檔頁(yè)面上展示的位置
圖片
enable
啟用或禁用Swagger文檔的生成,有時(shí)測(cè)試環(huán)境會(huì)開(kāi)放API文檔,但在生產(chǎn)環(huán)境則要禁用,可以根據(jù)環(huán)境變量控制是否顯示。
@Bean
public Docket docket(Environment environment) {
// 可顯示 swagger 文檔的環(huán)境
Profiles of = Profiles.of("dev", "test","pre");
boolean enable = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
.enable(enable)
.apiInfo(apiInfo()); // 文檔基礎(chǔ)配置
}
host
API文檔顯示的主機(jī)名稱或IP地址,即在測(cè)試執(zhí)行接口時(shí)使用的IP或域名。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.host("http://test.com") // 請(qǐng)求地址
.apiInfo(apiInfo()); // 文檔基礎(chǔ)配置
}
securitySchemes
配置API安全認(rèn)證方式,比如常見(jiàn)的在header中設(shè)置如Bearer、Authorization、Basic等鑒權(quán)字段,ApiKey對(duì)象中字段含義分別是別名、鑒權(quán)字段key、鑒權(quán)字段添加的位置。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.securitySchemes(
Arrays.asList(
new ApiKey("Bearer鑒權(quán)", "Bearer", "header"),
new ApiKey("Authorization鑒權(quán)", "Authorization", "header"),
new ApiKey("Basic鑒權(quán)", "Basic", "header")
)
);
}
這樣配置后,Swagger文檔將會(huì)包含一個(gè)Authorize按鈕,供用戶輸入我們?cè)O(shè)定的Bearer 、Authorization、Basic進(jìn)行身份驗(yàn)證。
圖片
securityContexts
securitySchemes方法中雖然設(shè)置了鑒權(quán)字段,但此時(shí)在測(cè)試接口的時(shí)候不會(huì)自動(dòng)在 header中加上鑒權(quán)字段和值,還要配置API的安全上下文,指定哪些接口需要進(jìn)行安全認(rèn)證。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.securitySchemes(
Arrays.asList(
new ApiKey("Bearer鑒權(quán)", "Bearer", "header"),
new ApiKey("Authorization鑒權(quán)", "Authorization", "header"),
new ApiKey("Basic鑒權(quán)", "Basic", "header")
)
)
.securityContexts(Collections.singletonList(securityContext()));
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(
Arrays.asList(
new SecurityReference("Authorization", new AuthorizationScope[0]),
new SecurityReference("Bearer", new AuthorizationScope[0]),
new SecurityReference("Basic", new AuthorizationScope[0])))
.build();
}
現(xiàn)在在測(cè)試調(diào)用API接口時(shí),header中可以正常加上鑒權(quán)字段和值了。
圖片
tags
為API文檔中的接口添加標(biāo)簽,標(biāo)簽可以用來(lái)對(duì)API進(jìn)行分類或分組,并提供更好的組織和導(dǎo)航功能。
@Bean
public Docket docket(Environment environment) {
return new Docket(DocumentationType.SWAGGER_2)
.tags(new Tag("小富接口", "小富相關(guān)的測(cè)試接口"))
}
授權(quán)登錄
出于對(duì)系統(tǒng)安全性的考慮,通常我們還會(huì)為API文檔增加登錄功能。
引入maven依賴
swagger的安全登錄是基于security實(shí)現(xiàn)的,引入相關(guān)的maven依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
登錄配置
application.yml文件中配置登錄swagger的用戶名和密碼。
spring:
security:
user:
name: admin
password: 123456
再次訪問(wèn)文檔就會(huì)出現(xiàn)如下的登錄頁(yè)
圖片
文檔注解
當(dāng)我們希望在Swagger文檔中提供詳細(xì)和完整的內(nèi)容時(shí),還可以使用其他許多Swagger內(nèi)置注解來(lái)進(jìn)一步豐富和定制API文檔。
@ApiIgnore
上邊我們提到可以根據(jù)指定路徑或者包路徑來(lái)提供API,也可以使用粒度更細(xì)的@ApiIgnore注解,來(lái)實(shí)現(xiàn)某個(gè)API在文檔中忽略。
@ApiIgnore
@GetMapping("/user2/{id}")
public User test2(@PathVariable Integer id, @RequestBody User user) {
return user;
}
@ApiModel
在我們的接口中,只要使用實(shí)體作為參數(shù)或響應(yīng)體,Swagger就會(huì)自動(dòng)掃描到它們,但你會(huì)發(fā)現(xiàn)目前這些實(shí)體缺乏詳細(xì)的描述信息。為了讓使用者通俗易懂,需要使用swagger提供的注解為這些實(shí)體添加詳細(xì)的描述。
圖片
@ApiModel注解的使用在實(shí)體類上,提供對(duì)Swagger Model額外信息的描述。
圖片
@ApiModelProperty
@ApiModelProperty 注解為實(shí)體類中的屬性添加描述,提供了字段名稱、是否必填、字段示例等描述信息。
@ApiModel(value = "用戶實(shí)體類", description = "用于存放用戶登錄信息")
@Data
public class User {
@ApiModelProperty(value = "用戶名字段", required = true, example = "#公眾號(hào):程序員小富")
private String name;
@ApiModelProperty(value = "年齡", required = true, example = "19")
private Integer age;
@ApiModelProperty(value = "郵箱", required = true, example = "#公眾號(hào):程序員小富")
private String email;
}
@Api
@Api 注解用于標(biāo)記一個(gè)控制器(controller)類,并提供接口的詳細(xì)信息和配置項(xiàng)。
- value:API 接口的描述信息,由于版本swagger版本原因,value可能會(huì)不生效可以使用description
- hidden:該 API 是否在 Swagger 文檔中隱藏
- tags:API 的標(biāo)簽,如果此處與 new Docket().tags 中設(shè)置的標(biāo)簽一致,則會(huì)將該 API 放入到這個(gè)標(biāo)簽組內(nèi)
- authorizations:鑒權(quán)配置,配合 @AuthorizationScope 注解控制權(quán)限范圍或者特定密鑰才能訪問(wèn)該API。
- produces:API的響應(yīng)內(nèi)容類型,例如 application/json。
- consumes:API的請(qǐng)求內(nèi)容類型,例如 application/json。
- protocols:API支持的通信協(xié)議。
@Api(value = "用戶管理接口描述",
description = "用戶管理接口描述",
hidden = false,
produces = "application/json",
consumes = "application/json",
protocols = "https",
tags = {"用戶管理"},
authorizations = {
@Authorization(value = "apiKey", scopes = {
@AuthorizationScope(scope = "read:user", description = "讀權(quán)限"),
@AuthorizationScope(scope = "write:user", description = "寫(xiě)權(quán)限")
}),
@Authorization(value = "basicAuth")
})
@RestController
public class TestController {
}
@ApiOperation
@ApiOperation該注解作用在接口方法上,用來(lái)對(duì)一個(gè)操作或HTTP方法進(jìn)行描述。
- value:對(duì)接口方法的簡(jiǎn)單說(shuō)明
- notes:對(duì)操作的詳細(xì)說(shuō)明。
- httpMethod:請(qǐng)求方式
- code:狀態(tài)碼,默認(rèn)為 200??梢詡魅敕蠘?biāo)準(zhǔn)的HTTP Status Code Definitions。
- hidden:在文檔中隱藏該接口
- response:返回的對(duì)象
- tags:使用該注解后,該接口方法會(huì)單獨(dú)進(jìn)行分組
- produces:API的響應(yīng)內(nèi)容類型,例如 application/json。
- consumes:API的請(qǐng)求內(nèi)容類型,例如 application/json。
- protocols:API支持的通信協(xié)議。
- authorizations:鑒權(quán)配置,配合 @AuthorizationScope 注解控制權(quán)限范圍或者特定密鑰才能訪問(wèn)該API。
- responseHeaders:響應(yīng)的header內(nèi)容
@ApiOperation(
value = "獲取用戶信息",
notes = "通過(guò)用戶ID獲取用戶的詳細(xì)信息",
hidden = false,
response = UserDto.class,
tags = {"用戶管理"},
produces = "application/json",
consumes = "application/json",
protocols = "https",
authorizations = {
@Authorization(value = "apiKey", scopes = {@AuthorizationScope(scope = "read:user", description = "讀權(quán)限")}),
@Authorization(value = "Basic")
},
responseHeaders = {@ResponseHeader(name = "X-Custom-Header", description = "Custom header", response = String.class)},
code = 200,
httpMethod = "GET"
)
@GetMapping("/user1")
public UserDto user1(@RequestBody User user) {
return new UserDto();
}
@ApiImplicitParams
@ApiImplicitParams注解用在方法上,以數(shù)組方式存儲(chǔ),配合@ApiImplicitParam 注解使用。
@ApiImplicitParam
@ApiImplicitParam注解對(duì)API方法中的單一參數(shù)進(jìn)行注解。
注意這個(gè)注解@ApiImplicitParam必須被包含在注解@ApiImplicitParams之內(nèi)。
- name:參數(shù)名稱
- value:參數(shù)的簡(jiǎn)短描述
- required:是否為必傳參數(shù)
- dataType:參數(shù)類型,可以為類名,也可以為基本類型(String,int、boolean等)
- paramType:參數(shù)的傳入(請(qǐng)求)類型,可選的值有 path、query、body、header、form。
@ApiImplicitParams({
@ApiImplicitParam(name = "用戶名", value = "用戶名稱信息", required = true, dataType = "String", paramType = "query")
})
@GetMapping("/user")
public String user(String name) {
return name;
}
@ApiParam()
@ApiParam()也是對(duì)API方法中的單一參數(shù)進(jìn)行注解,其內(nèi)部屬性和@ApiImplicitParam注解相似。
@GetMapping("/user4")
public String user4(@ApiParam(name = "主鍵ID", value = "@ApiParam注解測(cè)試", required = true) String id) {
return id;
}
@ApiResponses
@ApiResponses注解可用于描述請(qǐng)求的狀態(tài)碼,作用在方法上,以數(shù)組方式存儲(chǔ),配合 @ApiResponse注解使用。
@ApiResponse
@ApiResponse注解描述一種請(qǐng)求的狀態(tài)信息。
- code:HTTP請(qǐng)求響應(yīng)碼。
- message:響應(yīng)的文本消息
- response:返回類型信息。
- responseContainer:如果返回類型為容器類型,可以設(shè)置相應(yīng)的值。有效值為 "List"、 "Set"、"Map"其他任何無(wú)效的值都會(huì)被忽略。
@ApiResponses(value = {
@ApiResponse(code = 200, message = "@ApiResponse注解測(cè)試通過(guò)", response = String.class),
@ApiResponse(code = 401, message = "可能參數(shù)填的有問(wèn)題", response = String.class),
@ApiResponse(code = 404, message = "可能請(qǐng)求路徑寫(xiě)的有問(wèn)題", response = String.class)
})
@GetMapping("/user4")
public String user4(@ApiParam(name = "主鍵ID", value = "@ApiParam注解測(cè)試", required = true) String id) {
return id;
}
......
總結(jié)
盡管在面試中不會(huì)過(guò)多考察Swagger這類工具,但作為開(kāi)發(fā)者,養(yǎng)成良好的文檔規(guī)范習(xí)慣是非常重要的,無(wú)論使用Swagger還是其他文檔工具,編寫(xiě)清晰、詳盡的API文檔都應(yīng)該是我們的素養(yǎng)之一。
好了,今天的內(nèi)容分享就到這里了,感謝大家的收看,我們下篇見(jiàn)。
代碼示例
https://github.com/chengxy-nds/Springboot-Notebook/tree/master/springboot101/%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/springboot-swagger
我是小富,下期見(jiàn)~