SpringBoot整合Swagger2的示例
一、導(dǎo)入maven包
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
二、添加工具類
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.nvn.controller")) .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("SpringBoot整合Swagger") .description("SpringBoot整合Swagger,詳細(xì)信息......") .version("1.0") .build()); } }
三、添加注解
@RestController @Api(tags = "用戶管理相關(guān)接口") @RequestMapping("/user") public class UserController { @PostMapping("/") @ApiOperation("添加用戶的接口") @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "用戶名", defaultValue = "李四"), @ApiImplicitParam(name = "address", value = "用戶地址", defaultValue = "深圳", required = true) } ) public RespBean addUser(String username, @RequestParam(required = true) String address) { return new RespBean(); } @GetMapping("/") @ApiOperation("根據(jù)id查詢用戶的接口") @ApiImplicitParam(name = "id", value = "用戶id", defaultValue = "99", required = true) public User getUserById(@PathVariable Integer id) { User user = new User(); user.setId(id); return user; } @PutMapping("/{id}") @ApiOperation("根據(jù)id更新用戶的接口") public User updateUserById(@RequestBody User user) { return user; } }
四、注解說明
- @Api注解可以用來標(biāo)記當(dāng)前Controller的功能。
- @ApiOperation注解用來標(biāo)記一個(gè)方法的作用。
- @ApiImplicitParam注解用來描述一個(gè)參數(shù),可以配置參數(shù)的中文含義,也可以給參數(shù)設(shè)置默認(rèn)值,這樣在接口測試的時(shí)候可以避免手動(dòng)輸入。
- 如果有多個(gè)參數(shù),則需要使用多個(gè)@ApiImplicitParam注解來描述,多個(gè)@ApiImplicitParam注解需要放在一個(gè)@ApiImplicitParams注解中。
- @ApiImplicitParam注解中雖然可以指定參數(shù)是必填的,但是卻不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架內(nèi)必填,拋棄了Swagger2,這個(gè)限制就沒用了,所以假如開發(fā)者需要指定一個(gè)參數(shù)必填,@RequestParam(required = true)注解還是不能省略。
五、如果參數(shù)是一個(gè)對象,對于參數(shù)的描述可以放在實(shí)體類中。
@ApiModel public class User { @ApiModelProperty(value = "用戶id") private Integer id; @ApiModelProperty(value = "用戶名") private String username; @ApiModelProperty(value = "用戶地址") private String address; //getter/setter }
六、效果
附:如果我們的Spring Boot項(xiàng)目中集成了Spring Security,那么如果不做額外配置,Swagger2文檔可能會(huì)被攔截,此時(shí)只需要在Spring Security的配置類中重寫configure方法,添加如下過濾即可:
@Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/swagger-ui.html") .antMatchers("/v2/**") .antMatchers("/swagger-resources/**"); }
以上就是SpringBoot整合Swagger2的示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Swagger2的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vscode開發(fā)maven的javaweb項(xiàng)目并部署到tomcat及配置指南
這篇文章主要給大家介紹了關(guān)于vscode開發(fā)maven的javaweb項(xiàng)目并部署到tomcat及配置的相關(guān)資料,在vscode中創(chuàng)建maven項(xiàng)目,需要逐一操作下面的環(huán)節(jié),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12JPA @Query時(shí),無法使用limit函數(shù)的問題及解決
這篇文章主要介紹了JPA @Query時(shí),無法使用limit函數(shù)的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot開發(fā)實(shí)戰(zhàn)系列之動(dòng)態(tài)定時(shí)任務(wù)
在我們?nèi)粘5拈_發(fā)中,很多時(shí)候,定時(shí)任務(wù)都不是寫死的,而是寫到數(shù)據(jù)庫中,從而實(shí)現(xiàn)定時(shí)任務(wù)的動(dòng)態(tài)配置,下面這篇文章主要給大家介紹了關(guān)于SpringBoot開發(fā)實(shí)戰(zhàn)系列之動(dòng)態(tài)定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-08-08Springboot中的@ConditionalOnBean注解詳細(xì)解讀
這篇文章主要介紹了Springboot中的@ConditionalOnBean注解詳細(xì)解讀,@ConditionalOnMissingBean注解兩個(gè)類,一個(gè)Computer類,一個(gè)配置類,想要完成;如果容器中沒有Computer類,就注入備用電腦Computer類,如果有Computer就不注入,需要的朋友可以參考下2023-11-11Spring Boot超詳細(xì)分析啟動(dòng)流程
SpringBoot是Spring開源組織下的子項(xiàng)目,是Spring組件一站式解決方案,主要是簡化了使用Spring的難度,簡省了繁重的配置,提供了各種啟動(dòng)器,開發(fā)者能快速上手,這篇文章主要給大家介紹了關(guān)于Spring Boot啟動(dòng)流程知識點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-07-07SpringBoot使用Nacos配置中心的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用Nacos配置中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12java 學(xué)習(xí)筆記(入門篇)_java的安裝與配置
學(xué)習(xí)Java已經(jīng)很長時(shí)間了,由于基礎(chǔ)不好遇到問題就無從下手,所以,打算寫Java的隨手筆記來鞏固基礎(chǔ),加強(qiáng)學(xué)習(xí),接下來講解java的安裝,配置等,感興趣的朋友可以參考下2013-01-01Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng)
今天小編就為大家分享一篇關(guān)于Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03