SpringBoot基于Swagger2怎么构建API文档
导读:本文共1932.5字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 一、添加依赖<!--SpringBoot使用Swagger2构建API文档的依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version&g... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。一、添加依赖
<!--SpringBoot使用Swagger2构建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>
二、创建Swagger2配置类
packagecom.offcn.config;importorg.springframework.context.annotation.Configuration;importspringfox.documentation.builders.ApiInfoBuilder;importspringfox.documentation.builders.PathSelectors;importspringfox.documentation.builders.RequestHandlerSelectors;importspringfox.documentation.service.ApiInfo;importspringfox.documentation.spi.DocumentationType;importspringfox.documentation.spring.web.plugins.Docket;importspringfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration//表示该类为一个配置类,相当于spring中的xml配置文件@EnableSwagger2//开启在线文档publicclassSwaggerConfig{//1.声明api文档的属性privateApiInfoapiInfo(){returnnewApiInfoBuilder().title("SpringBoot中使用Swagger2构建RESTfulAPIs").description("优就业").termsOfServiceUrl("http://www.ujiuye.com/").contact("小刘同学").version("1.0").build();}//配置核心配置信息publicDocketcreateRestApi(){returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.offcn.controller")).paths(PathSelectors.any()).build();}}
三、修改Controller 增加文档注释
通过@ApiOperation注解来给API增加说明
通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明
packagecom.offcn.controller;importcom.offcn.dao.UserDao;importcom.offcn.entity.User;importio.swagger.annotations.ApiImplicitParam;importio.swagger.annotations.ApiImplicitParams;importio.swagger.annotations.ApiOperation;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.DeleteMapping;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RequestMapping("/rest")@RestControllerpublicclassRestFulController{@AutowiredprivateUserDaouserDao;@GetMapping("/getUserById")@ApiOperation(value="查找指定id用户信息",notes="根据id查找用户信息")@ApiImplicitParams({@ApiImplicitParam(name="id",value="用户ID",required=true,dataType="Integer"),})publicUsergetUserById(Integerid){Useruser=userDao.getOne(id);returnuser;}@DeleteMapping("/del")@ApiOperation(value="删除指定id用户信息",notes="根据id删除用户信息")@ApiImplicitParams({@ApiImplicitParam(name="id",value="用户ID",required=true,dataType="Integer"),})publicStringdelUserById(Integerid){userDao.deleteById(id);return"success";}}
四、查看Swagger2文档
重启项目
访问:
http://localhost:8080/swagger-ui.html
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
SpringBoot基于Swagger2怎么构建API文档的详细内容,希望对您有所帮助,信息来源于网络。