springboot集成swagger、knife4j及常用注解的使用
1. 集成swagger2
1.1 引入依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
1.2 添加Swagger配置
關(guān)鍵 RequestHandlerSelectors.basePackage(“com.gz”)
@Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInfo()) .select() // 要掃描的API(Controller)基礎(chǔ)包 .apis(RequestHandlerSelectors.basePackage("com.gz")) .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInfo() { Contact contact = new Contact("gz","",""); return new ApiInfoBuilder() .title("測(cè)試swagger-springbootdemo API文檔") .description("springbootdemo后臺(tái)api") .contact(contact) .version("1.0.0").build(); } }
通常情況下,swagger配置放在common模塊中,別的模塊需要集成swagger時(shí),只需要引入common模塊就行,因此需要將SwaggerConfiguration類在META-INF/spring.facories文件里進(jìn)行配置,這樣才能將SwaggerConfiguration類注冊(cè)進(jìn)IOC容器。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.gzdemo.springbootdemo.config.SwaggerConfiguration
但是如果SwaggerConfiguration類在啟動(dòng)類的同級(jí)目錄或子目錄下,就不需要此項(xiàng)配置了。
1.3 controllers和models
- TestController
@RequestMapping("/test") @RestController public class TestController { @GetMapping("/hello") public String hello(){ return "hello"; } @PostMapping("/addUser") public Map addUser(@RequestBody UserDto userDto){ Map<String, Object> result = new HashMap<>(); UserDto userDto1 = new UserDto(); userDto1.setName("ceshi"); userDto1.setAge(23); userDto1.setPhone("15700000001"); userDto1.setEmail("111@qq.com"); result.put("data",userDto1); result.put("msg","添加成功"); return result; } }
- UserDto
@Data public class UserDto { private String name; private String phone; private Integer age; private String email; }
1.4 瀏覽器訪問
瀏覽器訪問 http://localhost:8080/swagger-ui.html
2. Swagger常用注解
2.1 Swagger常用注解
在Java類中添加Swagger的注解即可生成Swagger接口文檔,常用Swagger注解如下:
- @Api:修飾整個(gè)類,描述Controller的作用
- @ApiOperation:描述一個(gè)類的一個(gè)方法,或者說一個(gè)接口
- @ApiParam:?jiǎn)蝹€(gè)參數(shù)的描述信息
- @ApiModel:用對(duì)象來接收參數(shù)
- @ApiModelProperty:用對(duì)象接收參數(shù)時(shí),描述對(duì)象的一個(gè)字段
- @ApiResponse:HTTP響應(yīng)其中1個(gè)描述
- @ApiResponses:HTTP響應(yīng)整體描述
- @ApiIgnore:使用該注解忽略這個(gè)API
- @ApiError :發(fā)生錯(cuò)誤返回的信息
- @ApiImplicitParam:一個(gè)請(qǐng)求參數(shù)
- @ApiImplicitParams:多個(gè)請(qǐng)求參數(shù)的描述信息
- @ApiImplicitParam屬性:
屬性 | 取值 | 作用 |
---|---|---|
paramType | 查詢參數(shù)類型 | |
path | 以地址的形式提交數(shù)據(jù) | |
query | 直接跟參數(shù)完成自動(dòng)映射賦值 | |
body | 以流的形式提交 僅支持POST | |
header | 參數(shù)在request headers 里邊提交 | |
form | 以form表單的形式提交 僅支持POST | |
dataType | 參數(shù)的數(shù)據(jù)類型 只作為標(biāo)志說明,并沒有實(shí)際驗(yàn)證 | |
Long | ||
String | ||
name | 接收參數(shù)名 | |
value | 接收參數(shù)的意義描述 | |
required | 參數(shù)是否必填 | |
true | 必填 | |
false | 非必填 | |
defaultValue | 默認(rèn)值 |
2.2 測(cè)試
我們?cè)赥estController和UserDto中添加Swagger注解,代碼如下所示:
@RequestMapping("/test") @RestController @Api(value = "測(cè)試控制器",tags = "test") public class TestController { @ApiOperation("用戶測(cè)試hello") @GetMapping("/hello") public String hello(){ return "hello"; } @ApiOperation("添加用戶") @PostMapping("/addUser") public Map addUser(@RequestBody UserDto userDto){ Map<String, Object> result = new HashMap<>(); UserDto userDto1 = new UserDto(); userDto1.setName("ceshi"); userDto1.setAge(23); userDto1.setPhone("15700000001"); userDto1.setEmail("111@qq.com"); result.put("data",userDto1); result.put("msg","添加成功"); return result; } } @Data public class UserDto { @ApiModelProperty(value="姓名",required = true) private String name; @ApiModelProperty(value="手機(jī)號(hào)",required = true) private String phone; @ApiModelProperty(value="年齡",required = true) private Integer age; @ApiModelProperty(value="郵箱",required = true) private String email; }
啟動(dòng)應(yīng)用,訪問 http://localhost:8080/swagger-ui.html
3. 集成knife4j
3.1 添加依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
3.2 添加注解
- 在SwaggerConfiguration類上添加 @EnableKnife4j
- 在SwaggerConfiguration類上添加@Import(BeanValidatorPluginsConfiguration.class),可能會(huì)報(bào)錯(cuò),可以不用加這個(gè),也不知道加了有什么用
3.3 瀏覽器訪問
http://localhost:8080/doc.html
4. Swagger常用注解案例
- @Api
@RestController @Api(tags = "課表的相關(guān)接口") @RequestMapping("/lessons") public class LearningLessonController { }
- @ApiOperation
@GetMapping("/now") @ApiOperation("查詢我正在學(xué)習(xí)的課程") public LearningLessonVO queryMyCurrentLesson() { return lessonService.queryMyCurrentLesson(); }
- @ApiParam
@ApiOperation("查詢指定課程的學(xué)習(xí)記錄") @GetMapping("/course/{courseId}") public LearningLessonDTO queryLearningRecordByCourse( @ApiParam(value = "課程id", example = "2") @PathVariable("courseId") Long courseId){ return recordService.queryLearningRecordByCourse(courseId); }
- @ApiModel
- @ApiModelProperty
@Data @ApiModel(description = "學(xué)習(xí)記錄") public class LearningRecordFormDTO { @ApiModelProperty("小節(jié)類型:1-視頻,2-考試") @NotNull(message = "小節(jié)類型不能為空") @EnumValid(enumeration = {1, 2}, message = "小節(jié)類型錯(cuò)誤,只能是:1-視頻,2-考試") private SectionType sectionType; @ApiModelProperty("課表id") @NotNull(message = "課表id不能為空") private Long lessonId; @ApiModelProperty("對(duì)應(yīng)節(jié)的id") @NotNull(message = "節(jié)的id不能為空") private Long sectionId; @ApiModelProperty("視頻總時(shí)長(zhǎng),單位秒") private Integer duration; @ApiModelProperty("視頻的當(dāng)前觀看時(shí)長(zhǎng),單位秒,第一次提交填0") private Integer moment; @ApiModelProperty("提交時(shí)間") private LocalDateTime commitTime; }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot中使用Swagger的最全方法詳解
- SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程
- SpringBoot使用Swagger范例講解
- SpringBoot2.6.x升級(jí)后循環(huán)依賴及Swagger無法使用問題
- springboot使用swagger-ui 2.10.5 有關(guān)版本更新帶來的問題小結(jié)
- SpringBoot如何優(yōu)雅地使用Swagger2
- SpringBoot整合Swagger和Actuator的使用教程詳解
- 詳解如何在SpringBoot里使用SwaggerUI
- SpringBoot3使用Swagger3的示例詳解
相關(guān)文章
Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問題解決
這篇文章主要為大家介紹了Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java替換(新增)JSON串里面的某個(gè)節(jié)點(diǎn)操作
這篇文章主要介紹了Java替換(新增)JSON串里面的某個(gè)節(jié)點(diǎn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11在SpringBoot項(xiàng)目中整合攔截器的詳細(xì)步驟
在系統(tǒng)中經(jīng)常需要在處理用戶請(qǐng)求之前和之后執(zhí)行一些行為,例如檢測(cè)用戶的權(quán)限,或者將請(qǐng)求的信息記錄到日志中,即平時(shí)所說的"權(quán)限檢測(cè)"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項(xiàng)目中整合攔截器的相關(guān)資料,需要的朋友可以參考下2022-09-09使用BufferedReader讀取TXT文件中數(shù)值,并輸出最大值
這篇文章主要介紹了使用BufferedReader讀取TXT文件中數(shù)值,并輸出最大值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12springboot?去掉netflix?禁用Eureka的解決方法
這篇文章主要介紹了springboot?去掉netflix?禁用Eureka的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09