zoukankan      html  css  js  c++  java
  • JavaWeb项目中集成Swagger API文档

    1.增加依赖

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

    2.配置类SwaggerConfig.java

    package com.example.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    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
    @ComponentScan(basePackages = { "com.example.demo" })
    public class SwaggerConfig {
        ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("demo Web service APIs")
                    .description("")
                    .license("")
                    .licenseUrl("")
                    .termsOfServiceUrl("")
                    .version("1.0.0")
                    .build();
        }
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                    .build()
                    .apiInfo(apiInfo());
        }
    
    }

    3.启动类加 @EnableSwagger2 注解

    package com.example;
    
    import com.example.config.SupplyConfig;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableConfigurationProperties(SupplyConfig.class)
    @EnableSwagger2
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    4.接口类加@Api及接口方法加@ApiOperation注解

    package com.example.demo.controller;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/greet")
    @Api(value = "demo api")
    public class HelloWorldController{
    
        @ApiOperation(value = "问好")
        @RequestMapping(value = "helloworld",method = RequestMethod.GET)
        public String sayHello() {
            return "hello world cc";
        }
    }

    5.访问swagger-ui

    http://localhost:8080/swagger-ui.html (端口是spring-boot项目启动的端口),打开页面如下:

    发送get请求:

  • 相关阅读:
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之六 多点触控
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之九 定位
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之七 重力感应
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之五 保存数据的几种方式
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之八 照相机
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之三 Application 配置详解
    Adobe Flash Builder 4.5 Android Air 程序开发系列 之四 打开与关闭应用程序是的保存数据
    ADOBE FLASH BUILDER 4.6 IOS 开发之部署与调试
    [译] 高性能JavaScript 1至5章总结
    页签及盒子的web标准实现
  • 原文地址:https://www.cnblogs.com/cici20166/p/8763969.html
Copyright © 2011-2022 走看看