springboot 使用swagger自动生成API文档

栏目: Java · 发布时间: 6年前

内容简介:如今微服务是趋势,微服务下的web开发通常采用前后端分离的方式.前后端通过API进行交互,Swagger ui,可以生成能让前端开发人员预览且测试的API文档在SpringBoot中集成swagger,方法如下

如今微服务是趋势,微服务下的web开发通常采用前后端分离的方式.前后端通过API进行交互,Swagger ui,可以生成能让前端开发人员预览且测试的API文档

在SpringBoot中集成swagger,方法如下

  1. 将下面的依赖添加到Maven项目的pom.xml文件中。springfox-swagger2组件帮助我们自动生成描述API的json文件,而springfox-swagger-ui组件就是将这个json文件解析出来,用一种更友好的方式呈现出来。
<!-- swagger 相关依赖  -->
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>2.6.1</version>
 </dependency>
 <dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
     <version>2.6.1</version>
 </dependency>
  1. 添加Swaager2的配置类

    package com.jetlee.demo1_server.config;
    
    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;
    
    
    @Configuration
    public class Swagger2 {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.jetlee.demo1_server"))
                    .paths(PathSelectors.any())
                    .build();
        }
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springboot利用swagger构建api文档")
                    .description("简单优雅的restfun风格")
                    .termsOfServiceUrl("https://www.lijinpeng.top")
                    .version("1.0")
                    .build();
        }
    }
    
  2. 在Applocation入口中使用Swagger2 @EnableSwagger2

package com.jetlee.demo1_server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class Demo1ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(Demo1ServerApplication.class, args);
    }
}
  1. 在Controller类上添加@API注解,方法想添加@ApiOperation,以及@ApiImplicitParam参数信息
    package com.jetlee.demo1_server.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @Api("测试 swagger2")
    public class IndexController {
    
        @ApiOperation(value = "获取用户列表",notes = "获取用户列表")
        @GetMapping("/getUserList")
        public String getUserList(){
            return  "ok";
        }
        
        @ApiOperation(value = "按id查找用户",notes = "按id查找用户")
        @ApiImplicitParam(name="userId",value="用户id",paramType="query",required = true,dataType = "int")
        @GetMapping("/getUserById")
        public String getUserById(){
            return  "user";
        }
    }
    

这样就可以生成api文档,在浏览器输入 http://127.0.0.1:9091/swagger-ui.html 显示结果如下

springboot 使用swagger自动生成API文档

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网运营之道

互联网运营之道

金璞、张仲荣 / 电子工业出版社 / 2016-1 / 49.00

《互联网运营之道》内容由运营方法论切入,包括运营的江湖地位、运营的基本逻辑、运营的三大手段(内容运营、活动运营和用户运营)、数据统计方法,等等。虽然是讲方法论,但内容上却有着深入的运营逻辑思考和大量实战案例验证。在讲解了方法论之后,《互联网运营之道》逐步深入剖析如何反脆弱,如何做运营创新,以及如何从小到大切入细分市场,等等。 对于互联网公司来说,产品设计部门和研发部门保证了创意的实现,是从0......一起来看看 《互联网运营之道》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具