如何配置Spring Boot(Kotlin)应用程序以将/重定向到/swagger-ui.html?

时间:2019-01-16 20:48:42

标签: spring-boot kotlin swagger-2.0

我正在努力找出需要添加到SwaggerConfiguration中的内容,以使我的Spring Boot应用程序将请求自动重定向到/到/swagger-ui.html

当前的SwaggerConfiguration就像...

package com.climate.squirrel.web

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

@Configuration
@EnableSwagger2
class SwaggerConfiguration {

    @Bean
    open fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
}

在Java中,这是通过实现...

@Bean
public WebMvcConfigurerAdapter forwardToIndex() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("redirect:/swagger-ui.html");
        }
    };
}

当我尝试仅猜测Kotlin等效值时,

@Bean
fun forwardToIndex(): WebMvcConfigurerAdapter {
    return object : WebMvcConfigurerAdapter() {
        override fun addViewControllers(registry: ViewControllerRegistry) {
            registry.addViewController("/").setViewName("redirect:/swagger-ui.html")
        }
    }
}

我收到不了解如何解决的弃用警告。

w: /Users/robert.kuhar/dev/squirrel/src/main/kotlin/com/climate/squirrel/web/SwaggerConfiguration.kt: (6, 58): 'WebMvcConfigurerAdapter' is deprecated. Deprecated in Java
w: /Users/robert.kuhar/dev/squirrel/src/main/kotlin/com/climate/squirrel/web/SwaggerConfiguration.kt: (26, 27): 'WebMvcConfigurerAdapter' is deprecated. Deprecated in Java
w: /Users/robert.kuhar/dev/squirrel/src/main/kotlin/com/climate/squirrel/web/SwaggerConfiguration.kt: (27, 25): 'WebMvcConfigurerAdapter' is deprecated. Deprecated in Java

如何摆脱这些警告?

1 个答案:

答案 0 :(得分:0)

这根本不是Kotlin,它是Java的中间弃用语言:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html

当您根据Java 8默认方法对它进行“ Kotlinize”操作时...

package com.climate.squirrel.web

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import springfox.documentation.swagger2.annotations.EnableSwagger2
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket

@Configuration
@EnableSwagger2
class SwaggerConfiguration {

    @Bean
    open fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()


    @Bean
    fun forwardToIndex(): WebMvcConfigurer {
        return object : WebMvcConfigurer {
            override fun addViewControllers(registry: ViewControllerRegistry) {
                registry.addViewController("/").setViewName("redirect:/swagger-ui.html")
            }
        }
    }
}