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

How do I configure my Spring Boot (Kotlin) app to redirect / to /swagger-ui.html?

我正在努力找出我需要添加到 SwaggerConfiguration 中的内容以使我的 Spring 启动应用程序自动将请求重定向到 / 到 /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

如何摆脱这些警告?

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

当你 "Kotlinize" 根据 Java 8 个默认方法...

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")
            }
        }
    }
}