All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin-spring.homeController.mustache Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
package {{basePackage}}

import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
{{#sourceDocumentationProvider}}
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.io.Resource
import org.springframework.util.StreamUtils
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.GetMapping
{{/sourceDocumentationProvider}}
{{^sourceDocumentationProvider}}
{{#useSwaggerUI}}
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.GetMapping
{{/useSwaggerUI}}
{{/sourceDocumentationProvider}}
{{#reactive}}
import org.springframework.web.reactive.function.server.HandlerFunction
import org.springframework.web.reactive.function.server.RequestPredicates.GET
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.RouterFunctions.route
import org.springframework.web.reactive.function.server.ServerResponse
import java.net.URI
{{/reactive}}
{{#sourceDocumentationProvider}}
import java.nio.charset.Charset
{{/sourceDocumentationProvider}}

/**
 * Home redirection to OpenAPI api documentation
 */
@Controller
class HomeController {
{{#useSwaggerUI}}
{{^springDocDocumentationProvider}}
{{#sourceDocumentationProvider}}
    private val apiDocsPath = "/openapi.json"
{{/sourceDocumentationProvider}}
{{#springFoxDocumentationProvider}}
    private val apiDocsPath = "/v2/api-docs"
{{/springFoxDocumentationProvider}}
{{/springDocDocumentationProvider}}
{{/useSwaggerUI}}
{{#sourceDocumentationProvider}}
    private val yamlMapper = YAMLMapper()

    @Value("classpath:/openapi.yaml")
    private lateinit var openapi: Resource

    @Bean
    fun openapiContent(): String {
        return openapi.inputStream.use {
            StreamUtils.copyToString(it, Charset.defaultCharset())
        }
    }

    @GetMapping(value = ["/openapi.yaml"], produces = ["application/vnd.oai.openapi"])
    @ResponseBody
    fun openapiYaml(): String = openapiContent()

    @GetMapping(value = ["/openapi.json"], produces = ["application/json"])
    @ResponseBody
    fun openapiJson(): Any = yamlMapper.readValue(openapiContent(), Any::class.java)
{{/sourceDocumentationProvider}}
{{#useSwaggerUI}}
{{^springDocDocumentationProvider}}

    @GetMapping(value = ["/swagger-config.yaml"], produces = ["text/plain"])
    @ResponseBody
    fun swaggerConfig(): String = "url: $apiDocsPath\n"
{{/springDocDocumentationProvider}}
{{#reactive}}

    @Bean
    fun index(): RouterFunction = route(
        GET("/"), HandlerFunction {
            ServerResponse.temporaryRedirect(URI.create("swagger-ui.html")).build()
        })
{{/reactive}}
{{^reactive}}

    @RequestMapping("/")
    fun index(): String = "redirect:swagger-ui.html"
{{/reactive}}
{{/useSwaggerUI}}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy