久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot整合Swagger2的示例

瀏覽:2日期:2023-04-10 18:53:33

一、導(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@EnableSwagger2public 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)值,這樣在接口測(cè)試的時(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è)對(duì)象,對(duì)于參數(shù)的描述可以放在實(shí)體類中。

@ApiModelpublic class User { @ApiModelProperty(value = '用戶id') private Integer id; @ApiModelProperty(value = '用戶名') private String username; @ApiModelProperty(value = '用戶地址') private String address; //getter/setter}

六、效果

SpringBoot整合Swagger2的示例

附:如果我們的Spring Boot項(xiàng)目中集成了Spring Security,那么如果不做額外配置,Swagger2文檔可能會(huì)被攔截,此時(shí)只需要在Spring Security的配置類中重寫configure方法,添加如下過濾即可:

@Overridepublic 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的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 彭州市| 祁东县| 黄陵县| 林芝县| 兴宁市| 太仓市| 延寿县| 车致| 施秉县| 汨罗市| 唐海县| 八宿县| 安吉县| 霍邱县| 沾益县| 祁东县| 民丰县| 贺州市| 盐山县| 长岛县| 岳阳县| 昂仁县| 健康| 广西| 稷山县| 天津市| 汤阴县| 仙游县| 佛冈县| 监利县| 都匀市| 遵义市| 洛宁县| 石狮市| 运城市| 会理县| 湖北省| 通河县| 正宁县| 渑池县| 高邑县|