【问题标题】:Swagger for Kotlin为 Kotlin 招摇
【发布时间】:2017-07-25 13:11:13
【问题描述】:

有人在 Kotlin 中使用过 swagger 工具吗?

在我们的组织中,我们使用 Java 和 SpringMVC(@RestController 类)创建了大部分 REST 服务。我们使用 springfox 来生成 Swagger API 文档。 swagger JSON 表示也用于自动提供可搜索的服务目录,因此服务元数据的 swagger 格式对我们很重要。

一些开发团队现在开始使用 Kotlin。我们正在寻找与在 Kotlin 中使用 springfox 或其他 swagger 库相关的建议或 cmets。

【问题讨论】:

    标签: kotlin swagger


    【解决方案1】:

    这是带有 swagger 的示例 Spring Boot 应用程序:

    @RestController
    class MyController {
        @ApiOperation(value = "doc header...", notes = "detailed doc...")
        @RequestMapping(value = "/double", method = arrayOf(RequestMethod.GET))
        fun doubleValue(number: Int) = 2 * number
    }
    
    
    @Configuration
    @EnableSwagger2
    class SwaggerConfig {
        @Bean
        fun api(): Docket {
            return Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build()
        }
    }
    

    依赖是

    compile("io.springfox:springfox-swagger2:2.7.0")
    compile("io.springfox:springfox-swagger-ui:2.7.0")
    

    如果您浏览http://localhost:8080/swagger-ui.html,它就在那里......

    【讨论】:

      【解决方案2】:

      我最近也有类似的要求。结果,我创建了一个template project,它集成了 Kotlin、Webflux 和 Swagger。它提供交互式 API 文档和自动请求验证。

      看这里->https://github.com/cdimascio/kotlin-swagger-spring-functional-template

      验证功能正常。它是这样使用的:

      validate.request(req) {
        // Do stuff e.g. return a list of names 
        ok().body(Mono.just(listOf("carmine", "alex", "eliana")))
      }
      

      有身体

      validate.request(req).withBody(User::class.java) { body ->
        // Note that body is deserialized as User!
        // Now you can do stuff. 
        // For example, lets echo the request as the response 
        ok().body(Mono.just(body))
      }
      

      它利用 atlassian 提供的 openapi 2 和 3 验证。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 2018-07-20
        • 2015-01-09
        • 2020-02-24
        相关资源
        最近更新 更多