解決springboot引入swagger2不生效問題
今天遇到跟同事遇到一個(gè)由于失誤導(dǎo)致的問題,也可以說(shuō)比較難發(fā)現(xiàn)了.在此記錄一下(我們用的springboot是2.0.3,swagger是2.2.2)
問題描述:
swagger修改title,description等都不生效。并且啟動(dòng)springboot,沒有有去加載swagger的配置類。(在debug模式啟動(dòng))
經(jīng)過不斷的查找,發(fā)現(xiàn)了原因是:swagger的配置類的注解加錯(cuò)了。@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;
//注意這個(gè)注解跟請(qǐng)求對(duì)應(yīng)的@XxxMapping,要不然這個(gè)接口會(huì)生成好多方法
@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 = "用戶對(duì)象")
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ī)號(hào)",name = "phone")
private String phone;
一些注解的使用
@Api:一般用于Controller中,用于接口分組
@ApiOperation:接口說(shuō)明,用于api方法上。
@ApiImplicitParams:用在方法上包含一組參數(shù)說(shuō)明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面
paramType:參數(shù)放在哪個(gè)地方
header 請(qǐng)求參數(shù)的獲?。篅RequestHeader
query 請(qǐng)求參數(shù)的獲?。篅RequestParam
path(用于restful接口) 請(qǐng)求參數(shù)的獲?。篅PathVariable
body(不常用)
form(不常用)
name:參數(shù)名
dataType:參數(shù)類型
required:參數(shù)是否必須傳
value:參數(shù)的意思
defaultValue:參數(shù)的默認(rèn)值
@ApiResponses:用于表示一組響應(yīng)
@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息,例如”請(qǐng)求參數(shù)沒填好”
response:拋出異常的類
@ApiModel:描述一個(gè)Model的信息(這種一般用在post創(chuàng)建的時(shí)候,使用@RequestBody這樣的場(chǎng)景,請(qǐng)求參數(shù)無(wú)法使用@ApiImplicitParam注解進(jìn)行描述的時(shí)候)表明這是一個(gè)被swagger框架管理的model,用于class上
@ApiModelProperty :使用在實(shí)體類上的成員變量上,描述成員變量的含義。
以上就是解決springboot引入swagger2不生效問題的詳細(xì)內(nèi)容,更多關(guān)于springboot引入swagger2的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例
這篇文章主要介紹了MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-01-01
java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Java編譯錯(cuò)誤問題:需要class,interface或enum
這篇文章主要介紹了Java編譯錯(cuò)誤問題:需要class,interface或enum,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
IDEA 集成log4j將SQL語(yǔ)句打印在控制臺(tái)上的實(shí)現(xiàn)操作
這篇文章主要介紹了IDEA 集成log4j將SQL語(yǔ)句打印在控制臺(tái)上的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-02-02
SpringBoot使用spring retry重試機(jī)制的操作詳解
實(shí)際工作中由于網(wǎng)絡(luò)波動(dòng)等原因?qū)е麓a執(zhí)行失敗需要重新執(zhí)行,保證最終能夠完成業(yè)務(wù)功能,通常來(lái)說(shuō),會(huì)用try/catch,while循環(huán)或者定時(shí)任務(wù)重處理,但是這樣的做法缺乏統(tǒng)一性,要多寫很多代碼,spring-retry組件可以通過注解優(yōu)雅的實(shí)現(xiàn)重處理功能2024-12-12

