本文小编为大家详细介绍“SpringBoot基于Swagger2怎么构建API文档”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringBoot基于Swagger2怎么构建API文档”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
一、添加依赖
<!--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配置类
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//表示该类为一个配置类,相当于spring中的xml配置文件
@EnableSwagger2 //开启在线文档
public class SwaggerConfig {
//1.声明 api 文档的属性
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("优就业")
.termsOfServiceUrl("http://www.ujiuye.com/")
.contact("小刘同学")
.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注解来给参数增加说明
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="根据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="根据id删除用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
})
public String delUserById(Integer id){
userDao.deleteById(id);
return "success";
}
}
四、查看Swagger2文档
重启项目
访问:
http://localhost:8080/swagger-ui.html
读到这里,这篇“SpringBoot基于Swagger2怎么构建API文档”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.xuebuyuan.com/3286720.html