解決springboot引入swagger2不生效問題
今天遇到跟同事遇到一個由于失誤導(dǎo)致的問題,也可以說比較難發(fā)現(xiàn)了.在此記錄一下(我們用的springboot是2.0.3,swagger是2.2.2)
問題描述:
swagger修改title,description等都不生效。并且啟動springboot,沒有有去加載swagger的配置類。(在debug模式啟動)
經(jīng)過不斷的查找,發(fā)現(xiàn)了原因是:swagger的配置類的注解加錯了。@Configuration不小心寫成了@Configurable.
還有就是@EnableSwagger2注解只需要加在swagger配置類上
springboot引入swagger2的步驟:
①引入依賴
<!-- 引入swagger包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
②編寫Swagger2的配置類
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("Swagger2....")
.description("Swagger2")
.version("1.0")
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.build();
}
}
③在controller中添加注解:按需添加注解
@Controller
@RequestMapping("/user")
@Api(tags = "我的接口模塊")
public class UserController {
@Autowired
private UserService userService;
//注意這個注解跟請求對應(yīng)的@XxxMapping,要不然這個接口會生成好多方法
@GetMapping(value = "/getUserById")
@ResponseBody
@ApiOperation(value = "根據(jù)ID查詢User")
public User getUserById(@RequestParam(value = "id") int id){
return userService.getUserById(id);
}
}
④在model(pojo)上加注解,按需添加
@ApiModel(value = "用戶對象")
public class User {
@ApiModelProperty(value = "用戶ID", name = "userId")
private Integer userId;
@ApiModelProperty(value = "用戶姓名",name = "userName")
private String userName;
@ApiModelProperty(value = "用戶密碼",name = "password")
private String password;
@ApiModelProperty(value = "用戶手機(jī)號",name = "phone")
private String phone;
一些注解的使用
@Api:一般用于Controller中,用于接口分組
@ApiOperation:接口說明,用于api方法上。
@ApiImplicitParams:用在方法上包含一組參數(shù)說明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面
paramType:參數(shù)放在哪個地方
header 請求參數(shù)的獲?。篅RequestHeader
query 請求參數(shù)的獲取:@RequestParam
path(用于restful接口) 請求參數(shù)的獲?。篅PathVariable
body(不常用)
form(不常用)
name:參數(shù)名
dataType:參數(shù)類型
required:參數(shù)是否必須傳
value:參數(shù)的意思
defaultValue:參數(shù)的默認(rèn)值
@ApiResponses:用于表示一組響應(yīng)
@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息,例如”請求參數(shù)沒填好”
response:拋出異常的類
@ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時候)表明這是一個被swagger框架管理的model,用于class上
@ApiModelProperty :使用在實體類上的成員變量上,描述成員變量的含義。
以上就是解決springboot引入swagger2不生效問題的詳細(xì)內(nèi)容,更多關(guān)于springboot引入swagger2的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis 實現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例
這篇文章主要介紹了MyBatis 實現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Java編譯錯誤問題:需要class,interface或enum
這篇文章主要介紹了Java編譯錯誤問題:需要class,interface或enum,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
IDEA 集成log4j將SQL語句打印在控制臺上的實現(xiàn)操作
這篇文章主要介紹了IDEA 集成log4j將SQL語句打印在控制臺上的實現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot使用spring retry重試機(jī)制的操作詳解
實際工作中由于網(wǎng)絡(luò)波動等原因?qū)е麓a執(zhí)行失敗需要重新執(zhí)行,保證最終能夠完成業(yè)務(wù)功能,通常來說,會用try/catch,while循環(huán)或者定時任務(wù)重處理,但是這樣的做法缺乏統(tǒng)一性,要多寫很多代碼,spring-retry組件可以通過注解優(yōu)雅的實現(xiàn)重處理功能2024-12-12

