`
vtrtbb
  • 浏览: 358361 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring boot 中用Swagger2 构建API说明文档

    博客分类:
  • java
 
阅读更多

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信息使用方法

 

分享到:
评论

相关推荐

    Spring Boot中使用Swagger2构建强大的RESTful API文档

    为了解决上面这样的问题,本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。它既可以减少我们创建文档的工作量,同时说明内容又...

    Spring Boot集成 Swagger2 展现在线接口文档

    Spring Boot 集成 Swagger2 展现在线接口文档 Spring Boot 是一个基于 Java 的框架,用于快速构建生产级别的应用程序。Swagger 是一个流行的 API 文档工具,能够生成在线接口文档,帮助开发人员和调用接口的人员更...

    Spring Boot Swagger2 构建RESTful API

    在Spring Boot项目中集成Swagger2,可以帮助我们快速地构建和维护高质量的RESTful API。以下将详细讲解如何利用Spring Boot与Swagger2进行集成,并展示其主要功能和步骤。 **一、集成Swagger2** 1. 添加依赖:首先...

    spring boot 2.6.11+springcloud Swagger3构建微服务项目源码

    在Spring Boot应用中集成Swagger3,我们可以生成清晰的API文档,方便开发者理解和使用接口。Swagger UI允许我们在浏览器中直接测试API,极大地提高了开发效率和用户体验。Swagger3相比于之前的版本,提供了更多强大...

    spring boot 整合 swagger2

    Spring Boot 整合 Swagger2 是一个常见的需求,用于构建RESTful API的文档系统。Swagger2是一个流行的API开发工具,它可以自动生成API文档,方便开发者理解和使用API。在Spring Boot项目中整合Swagger2,可以让我们...

    Spring Boot 整合Swagger实现API管理-教案.pdf

    - springfox-swagger2:Swagger的核心依赖包,提供了Swagger API文档的生成能力。 - springfox-swagger-ui:提供了可视化的Swagger文档界面,使得开发者可以通过Web页面浏览API文档。 - swagger-bootstrap-ui:提供...

    spring Boot 集成swagger2全过程(代码包含集成Spring Security+ JWT)

    在本教程中,我们将深入探讨如何将Swagger2与Spring Boot集成,同时考虑到Spring Security和JWT(JSON Web Token)的安全机制。Swagger2是一个流行的API文档工具,它允许开发者以交互式方式展示和测试RESTful API。...

    Spring Boot 中使用 Swagger2 构建 RESTful APIs

    Spring Boot 中使用 Swagger2 构建 RESTful APIs Swagger 是一系列 RESTful API 的工具,通过 Swagger 可以获得项目的交互式文档,客户端 SDK 的自动生成等功能。Swagger 的目标是为 REST APIs 定义一个标准的、与...

    Spring Boot的自动化配置实现swagger2引入spring boot生成API文档.docx

    ### Spring Boot的自动化配置实现swagger2引入spring boot生成API文档 #### 一、Spring Boot与Swagger集成概述 在现代Web开发中,API文档对于确保良好的系统间通信至关重要。随着微服务架构的兴起,API文档的需求...

    Spring Boot中使用Swagger2构建RESTful APIs

    Swagger是一个广泛使用的...以上是在Spring Boot中使用Swagger2构建RESTful APIs的相关知识点。了解和掌握这些知识点,可以帮助开发者更高效地开发、测试和维护RESTful API,同时使得API文档的管理更加规范化和自动化。

    spring boot schedule swagger

    "spring boot schedule swagger"的主题涵盖了两个关键领域:Spring Boot的定时任务调度(Schedule)和Swagger API文档工具。本文将深入探讨这两个核心概念,并结合SQL语句和代码示例,为你提供全面的理解。 1. **...

    Spring Boot 2.7.5 集成 Swagger 3

    集成Swagger 3到Spring Boot项目中,可以帮助开发者更有效地管理和维护API,同时提供一个交互式的用户界面,让前端开发者能够轻松地了解和测试后端接口。 首先,要在Spring Boot 2.7.5项目中集成Swagger 3,我们...

    swagger导出静态API文档工具

    Swagger导出静态API文档工具是基于Swagger的一个实用工具,它是一个Maven工程,这意味着我们可以利用Maven的构建生命周期来自动化文档的生成过程。 首先,让我们了解Maven。Maven是一个项目管理工具,它通过读取...

    Spring Boot技术知识点:Spring Boot2.7以上支持使用Swagger3

    在Spring Boot 2.7及以上版本,它开始支持Swagger 3,这是一个强大的API文档工具,帮助开发者构建清晰、易于理解的RESTful API接口。 Swagger 3,也称为OpenAPI Specification 3.0,是Swagger的最新版本,基于...

    Spring boot+swagger(demo)

    - 引入依赖:首先,你需要在Spring Boot的`pom.xml`文件中引入Swagger的相关依赖,如`springfox-swagger2`和`springfox-swagger-ui`。 - 配置Swagger:创建一个配置类,使用`@EnableSwagger2`注解开启Swagger功能...

    Swagger3生成API文档配置(Demo)

    总结,Swagger3是Spring Boot项目中用于生成API文档的强大工具,通过注解和配置,我们可以轻松地创建和管理RESTful API的文档。通过运行应用,我们可以利用Swagger UI进行接口的测试和调试,极大地提高了开发效率和...

    spring-boot集成swagger

    将 Swagger 集成到 Spring Boot 中,可以实现自动化地生成 API 文档,提高开发效率。 **一、Spring Boot 集成 Swagger 前置知识** 1. **Spring Boot**:基于 Spring 框架的快速开发工具,提供自动配置、内嵌服务器...

    Spring Boot整合swagger的使用方法详解教程.docx

    Spring Boot 整合 Swagger 是一种常见的方式,用于构建RESTful API的交互式文档。Swagger 提供了一种标准化的方式来描述 RESTful API,使得开发者能够轻松地理解接口的使用方法,并进行在线调试。以下是对如何在 ...

    swagger整合Spring Boot生成Restful接口文档

    而Swagger是目前最流行的接口文档解决方案,本文主要通过代码实战的方式讲解Spring Boot 和Swagger集成生成Restful接口文档。教程参见 http://blog.csdn.net/zjx2016/article/details/74407832

Global site tag (gtag.js) - Google Analytics