zoukankan      html  css  js  c++  java
  • swagger2文档的步骤

    传统方法

      a: 创建web工程

      b: 配置springmvc web.xml

      c: 编写controller

      d :部署tomact

    springboot

      1 创建springboot 工程 必须继承spring-boot-stater-parent

      导依赖

    <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>

    配置文件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
    @EnableSwagger2 //开启在线文档
    public class SwaggerConfig {
        //声明api 文档属性 构建器
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("springboot中使用在线文档构建RestFul风格Apis")
                    .description("哈哈")
                    .termsOfServiceUrl("http://www.baidu.com/")
                    .contact("0708java")
                    .version("2.0")
                    .build();
    
        }
        //配置核心配置信息
        public Docket creatRestApi(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.offcn.springbootdemo.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    }

      2 编写controller

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/work")
    public class WorkController {
    
        @RequestMapping(value = "/first")
        public String getList(@RequestParam("id") Integer id){
    
            return "hahahah"+id;
        }
    }

      3 主启动类

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbootdemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootdemoApplication.class, args);
        }
    
    }

      4 启动main

  • 相关阅读:
    Spring AOP @Before @Around @After 等 advice 的执行顺序
    web服务启动spring自动执行ApplicationListener的用法
    Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
    ApplicationContextAware使用理解
    查询oracle比较慢的session和sql
    使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题
    C#控制台程序中处理2个关闭事件的代码实例
    mysql优化----第一篇:综述
    HMM 隐马尔科夫模型
    EM 期望最大化算法
  • 原文地址:https://www.cnblogs.com/cjh-code/p/11801892.html
Copyright © 2011-2022 走看看