摇摇欲坠的Kotlin

时间:2017-07-25 13:11:13

标签: kotlin swagger

有没有人使用过Kotlin的招摇工具?

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

一些开发团队现在开始使用Kotlin。我们正在寻找与使用Spring或其他swagger lib与Kotlin相关的建议或评论。

2 个答案:

答案 0 :(得分:7)

以下是带有招摇的示例弹簧启动应用程序:

@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,那就是......

答案 1 :(得分:1)

我最近有类似的要求。结果,我创建了一个集成了Kotlin,Webflux和Swagger的template project。它提供交互式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提供的v2 swagger验证。