maven:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>
配置文件:
import org.springframework.context.annotation.Bean; 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 @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket buildDocket() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInf()) .select() .apis(RequestHandlerSelectors.basePackage("com.site.portal.web"))//要扫描的API(Controller)基础包 .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInf() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2 UI构建API文档") .contact("测试下") .version("1.0") .build(); } }
controller 类中:
import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @Api(value = "城市服务",description="简单的事例") @RestController @RequestMapping("/cities") public class CityController { @Autowired private CityService cityService; @ApiOperation("城市列表") @RequestMapping public PageInfo<City> getAll(City city) { List<City> countryList = cityService.getAll(); return new PageInfo<City>(countryList); } @ApiOperation("添加城市") @RequestMapping(value = "/add") public City add() { return new City(); } @RequestMapping(value = "/view/{id}") public City view(@PathVariable Integer id) { ModelAndView result = new ModelAndView(); City city = cityService.getById(id); return city; } @ApiOperation("删除城市") @RequestMapping(value = "/delete/{id}") public ModelMap delete(@PathVariable Integer id) { ModelMap result = new ModelMap(); cityService.deleteById(id); result.put("msg", "删除成功!"); return result; } @ApiOperation("保存城市") @RequestMapping(value = "/save") public void save() { ModelMap result = new ModelMap(); City city=new City(); city.setName("beijing"+System.currentTimeMillis()); city.setState("1"); String msg = city.getId() == null ? "新增成功!" : "更新成功!"; cityService.save(city); result.put("city", city); result.put("msg", msg); } }
重新打包部署你的项目到WEB服务器,
访问地址
http://localhost:8080/your-contextpath/api-docs即可看到注解生成的API说明
访问地址
http://localhost:8080/your-contextpath/swagger-ui.html即可看到API信息使用方法
相关推荐
为了解决上面这样的问题,本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。它既可以减少我们创建文档的工作量,同时说明内容又...
Spring Boot Swagger2 构建RESTful API
Spring Boot集成 Swagger2 展现在线接口文档
swagger-spring-boot-starter该项目主要利用Spring Boot的自动化配置特性来实现快速的将swagger2引入spring boot应用来生成API文档,简化原生使用swagger2的整合代码。 swagger-bootstrap-ui是springfox- swagger的...
Spring Boot 整合Swagger实现API管理_教案.pdf
spring boot 2.6.11 + spring cloud + Swagger3.0.0 微服务项目源码
一份关于使用Swagger自动生成API说明文档的开发文档。
spring boot 整合 swagger2,步骤详细。包含waven依赖。配置,注解事例
Spring Boot 中使用 Swagger2 构建 RESTful APIs
而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832
文档为使用Spring Boot集成Swagger使用指南,主要包括swagger的组成,集成过程,以及常用注解的解释,通过文档应该可以使用swagger展示自己撰写的REST接口,进而更好的进行前后端交互。
Spring boot整合swagger测试工具的一个demo,只需要修改数据库配置就能使用
Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档 Spring Boot 2.x基础教程:JSR-303实现请求参数校验 Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解 Spring Boot 2.x基础教程:Swagger...
工具是一个maven工程,可通过maven命令导出静态接口文档,具体操作步骤见附件中的ReadMe.txt
Spring Boot 2.7.5 集成 Swagger 3,用于前后端分离开发,前端需要经常查看后端接口定义,以及接口的响应,所以集成可视化、交互式的 api 文档至关重要。此外还可以测试后端接口。
spring boot + swagger2集成api接口文档,springBoot+swagger+mysql 搭建的一个项目。可以启动。可供参考使用; Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是...
个人备份; 第 1-3 课 Spring Boot 依赖环境和项目结构介绍 第 1-4 课:写一个 Hello World 来感受 Spring Boot/hello ...第 2-9 课:Spring Boot 中使用 Swagger2 构建 RESTful APIs/spring-boot-sw
springboot整合swagger构建Api文档,对于整理在线文档这是一大利器,相对于ssm整合是真的简单方便,不管是不是新手,一看就能懂,对于与前端交互,那是相当的便捷。
spring-boot,swagger2,生成html及中文pdf示例。 https://download.csdn.net/download/lihuaijun/10313631 asciidoctorj-pdf支持中文生成 这个资源的评论里,有人说运行出错,我把例子发上来
java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java 使用 Swagger 创建一个Spring Boot 的 Web 服务java...