SpringBoot基于Swagger2構(gòu)建API文檔過程解析
一、添加依賴
<!--SpringBoot使用Swagger2構(gòu)建API文檔的依賴--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
二、創(chuàng)建Swagger2配置類
package com.offcn.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration//表示該類為一個(gè)配置類,相當(dāng)于spring中的xml配置文件 @EnableSwagger2 //開啟在線文檔 public class SwaggerConfig { //1.聲明 api 文檔的屬性 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs") .description("優(yōu)就業(yè)") .termsOfServiceUrl("http://www.ujiuye.com/") .contact("小劉同學(xué)") .version("1.0") .build(); } //配置核心配置信息 public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.offcn.controller")) .paths(PathSelectors.any()) .build(); } }
三、修改Controller 增加文檔注釋
通過@ApiOperation注解來給API增加說明
通過@ApiImplicitParams@ApiImplicitParam注解來給參數(shù)增加說明
package com.offcn.controller; import com.offcn.dao.UserDao; import com.offcn.entity.User; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/rest") @RestController public class RestFulController { @Autowired private UserDao userDao; @GetMapping("/getUserById") @ApiOperation(value="查找指定id用戶信息", notes="根據(jù)id查找用戶信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"), }) public User getUserById(Integer id){ User user = userDao.getOne(id); return user; } @DeleteMapping("/del") @ApiOperation(value="刪除指定id用戶信息", notes="根據(jù)id刪除用戶信息") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Integer"), }) public String delUserById(Integer id){ userDao.deleteById(id); return "success"; } }
四、查看Swagger2文檔
重啟項(xiàng)目
訪問:
http://localhost:8080/swagger-ui.html
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決
這篇文章主要介紹了mybatis-plus IdWorker生成的Id和返回給前臺(tái)的不一致的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03MyBatis中${}?和?#{}?有什么區(qū)別小結(jié)
${}?和?#{}?都是?MyBatis?中用來替換參數(shù)的,它們都可以將用戶傳遞過來的參數(shù),替換到?MyBatis?最終生成的?SQL?中,但它們區(qū)別卻是很大的,今天通過本文介紹下MyBatis中${}?和?#{}?有什么區(qū)別,感興趣的朋友跟隨小編一起看看吧2022-11-11Idea中如何調(diào)出Run dashboard 或services窗口
這篇文章主要介紹了Idea中如何調(diào)出Run dashboard 或services窗口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn)
角色權(quán)限校驗(yàn),是保證接口安全必備的能力:有權(quán)限才可以操作,所以,一般對(duì)于這種通用邏輯,推薦不與主業(yè)務(wù)邏輯耦合,那么怎么來解耦,那么本文小編就給大家詳細(xì)講解如何使用攔截器Interceptor實(shí)現(xiàn)統(tǒng)一角色權(quán)限校驗(yàn),需要的朋友可以參考下2023-07-07