如何生成界面的草率文档?

时间:2020-03-13 18:40:12

标签: spring-boot swagger swagger-ui swagger-2.0

我已经用谷歌搜索了,但是所有关于swagger文档的示例都使用了类。 我想包括接口,因为读者对API而不是实现感兴趣。

这是我的代码:

包括所需的Maven依赖项:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

我的SpringBootApplication:

package com.manojk.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

我的RestController界面

package com.manojk.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/") // this annotation needed for swagger-ui to work
public interface HelloWorldControllerInterface {
    @GetMapping
    String helloWorld();
}

实施

package com.manojk.demo;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Component // this annotation needed to initialize this controller
public class HelloWorldController implements HelloWorldControllerInterface {
    @Override
    public String helloWorld(){
        return "Hello World";
    }
}

所需的swagger配置均支持此操作:

package com.manojk.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * Configuration class for Swagger REST API documentation
 *
 */
@Configuration
@EnableSwagger2
/**
 * Run nad hit http://localhost:8080/swagger-ui.html to see it working
 */
public class SwaggerConfig {

  /**
   * @return Swagger Docker
   */
  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            // .apis(RequestHandlerSelectors.basePackage("com.siemens.oss.domain.controller.service")) # does not work for interfaces
            .apis(RequestHandlerSelectors.basePackage("com.manojk.demo")) // works for implementations
            .paths(regex("/.*"))
            .build();
  }
}

该代码可作为Maven项目在https://github.com/MKhotele/spring-examples/tree/master/demo上获得。

http://localhost:8080/swagger-ui.html显示“ Hello World Controller”;但“ Hello World控制器接口”一无所获。

是否可以使招摇的包括接口?怎么样?

1 个答案:

答案 0 :(得分:0)

Segii Zhevzhyk在注释中提供了提示。谢谢!

这不可能,因此不可能。

OpenAPI specification(formerly Swagger Specification)不仅与REST API的规范有关。这也与他们互动有关。接口永远不能公开端点进行交互。但这只是一个实现。

Swagger是围绕OpenAPI构建的一组开源工具 可以帮助您设计,构建,记录和使用的规范 REST API

Introduction to OpenAPI-Specification中所述,

OpenAPI规范(OAS)定义了与语言无关的标准 与RESTful API的接口,使人类和计算机都可以 无需访问即可发现并了解服务的功能 源代码,文档或通过网络流量检查。 正确定义后,消费者可以了解并与 远程服务,并且实现逻辑最少。

相关问题