解決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 = "用戶手機號",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中,一般用于表達一個錯誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息,例如”請求參數(shù)沒填好”
response:拋出異常的類
@ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候,使用@RequestBody這樣的場景,請求參數(shù)無法使用@ApiImplicitParam注解進行描述的時候)表明這是一個被swagger框架管理的model,用于class上
@ApiModelProperty :使用在實體類上的成員變量上,描述成員變量的含義。
以上就是解決springboot引入swagger2不生效問題的詳細(xì)內(nèi)容,更多關(guān)于springboot引入swagger2的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot 對象存儲 MinIO的詳細(xì)過程
MinIO 是一個基于 Go 實現(xiàn)的高性能、兼容 S3 協(xié)議的對象存儲,它適合存儲海量的非結(jié)構(gòu)化的數(shù)據(jù),這篇文章主要介紹了SpringBoot 對象存儲 MinIO,需要的朋友可以參考下2023-07-07SpringBoot 統(tǒng)一異常處理的實現(xiàn)示例
本文主要介紹了SpringBoot 統(tǒng)一異常處理的實現(xiàn)示例,目的就是在異常發(fā)生時,盡可能地減少破壞,下面就來介紹一下,感興趣的可以了解一下2024-07-07Spring Native項目實戰(zhàn)(體驗79毫秒啟動springboot應(yīng)用)
Spring Native是Spring提供的、制作native image的技術(shù)方案,本篇主要內(nèi)容是開發(fā)springboot應(yīng)用再構(gòu)建為native image的方法,通過Spring Native項目實戰(zhàn)讓大家體驗79毫秒啟動springboot應(yīng)用,感興趣的朋友跟隨小編一起看看吧2021-05-05