swagger+代码生成器

Swagger+代码生成器

什么是Swagger

Swagger是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的Web服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集成到服务器端的代码,允许API始终保持同步

作用

  • 连接文档在线自动生成
  • 功能测试

Swagger是一个开源项目,其中主要要项目如下:

  • Swagger-tools:提供各种与Swagger进行集成和交互的工具。例如模式检验,Swagger 1.2文档转换成Swagger 2.0文档等功能。
  • Swagger-core:用于Java / Scala的Swagger实现。与JAX-RS(Jersey,Resteasy,CXF …),Servlets和Play框架进行集成。
  • Swagger-js:用于JavaScript的Swagger实现。
  • Swagger-node-express:Swagger模块,用于node.js的Express web应用框架。
  • Swagger-ui:一个无依赖的HTML,JS和CSS集合,可以为Swagger兼容API动态生成优雅文档。
  • Swagger-codegen:一个模板驱动引擎,通过分析用户Swagger资源声明以各种语言生成客户端代码
Spring和Swagger

在Spring中集成Swagger会使用到springfox-swagger,它对Spring和Swagger的使用进行了整合

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${springfox.swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${springfox.swagger.version}</version>
</dependency>

/**
 * Swagger2配置类
 * 在与spring boot集成时,放在与Application.java同级的目录下。
 * 或者通过 @Import 导入配置
 */
@Configuration
@EnableSwagger2
public class Swagger2 {
    
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.turbo.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("")
                .termsOfServiceUrl("")
                .contact("zou", "", "zouxq412@foxmail.com")
                .version("1.0")
                .build();
    }
}

API注解–Swagger_Demo
package cn.kgc.swagger.controller;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * User:26918
 * Time:9:46
 *
 * @author ZhaoBinrui on 2020/9/8
 */
@EnableSwagger2
@ComponentScan(basePackages = {"cn.kgc.swagger.controller"})
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo())
                .select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    public ApiInfo getApiInfo(){
        return new ApiInfoBuilder().title("Swagger 测试")
                .contact(new Contact("赵斌瑞","","15333403870@163.com"))
                .version("V 1.0").build();
    }
}
package cn.kgc.swagger.controller;

import cn.kgc.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * User:26918
 * Time:9:36
 *
 * @author ZhaoBinrui on 2020/9/8
 */
@Controller
@RequestMapping("/test")
@Api(produces = "SwaggerTest 测试")
public class TestController {

    @RequestMapping(value = "/demo1",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "测试接口一")
    public String testDemo1(){
        return "测试数据";
    }

    @RequestMapping(value = "/demo2",method = RequestMethod.POST)
    @ApiOperation(value = "基本接口测试")
    public void textDemoBaseDate(String name){
        System.out.println("基本数据类型测试");
    }

    @RequestMapping(value = "/demo3",method = RequestMethod.GET)
    @ApiOperation(value = "对象测试")
    @ResponseBody
    public User testObject(User user){
        return user;
    }
}
@ApiOperation

在指定的(路由)路径上,对一个操作或HTTP方法进行描述。具有相同路径的不同操作会被归组为同一个操作对象。不同的HTTP请求方法及路径组合构成一个唯一操作。此注解的属性有:

  • value对操作的简单说明,长度为120个字母,60个汉字。
  • notes对操作的详细说明。
  • httpMethod HTTP请求的动作名,可选值有:“ GET”,“ HEAD”,“ POST”,“ PUT”,“ DELETE”,“ OPTIONS”和“ PATCH”。
  • 代码默认为200,有效值必须符合标准的HTTP状态代码定义

T”,“ HEAD”,“ POST”,“ PUT”,“ DELETE”,“ OPTIONS”和“ PATCH”。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值