我可以让 /swagger-ui.html 重定向到 /swagger-ui/

Can I get /swagger-ui.html to redirect to /swagger-ui/

在 springfogx-swagger-ui 3.0.0,我的 Spring 启动应用程序在 http://myapp.example.com:8080/swagger-ui/. My users are used to seeing that URL as http://myapp.example.com:8080/swagger-ui.html. Is there a way I can set up a redirect or something to let users who "know" http://myapp.example.com:8080/swagger-ui.html be routed to the "correct" URL http://myapp.example.com:8080/swagger-ui/?

以 Swagger UI 结束

我在“通常”Spring 启动方式中得到了 swagger 和 swagger-ui

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>

无论如何,这可能是 Spring 引导问题,而不是 Spring Fox 问题。重定向似乎是简单明了的解决方案。

嗯。这行得通。


/**
 * Redirects requests for swagger-ui.html to the /swagger-ui/ endpoint.
 */
@ApiIgnore
@RestController
public class SwaggerHtmlRedirector {
  @RequestMapping(
      method = RequestMethod.GET,
      path = "/swagger-ui.html")
  public RedirectView redirectWithUsingRedirectView() {
    return new RedirectView("/swagger-ui/");
  }
}

来源是https://www.baeldung.com/spring-swagger-hiding-endpoints

还有更好的吗?

是的,重定向似乎是最简单的方法。如果您已有自定义 MVC 配置,则可以添加 addRedirectViewController 行。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class MyWebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/swagger-ui.html", "/swagger-ui/");
    }

}