【问题标题】:How to make swagger-ui.html as default context path如何使 swagger-ui.html 作为默认上下文路径
【发布时间】:2019-10-29 01:33:46
【问题描述】:

我有一个默认上下文路径为“/”的应用程序,但我想将默认上下文路径设为 swagger-ui.html。

假设如果我的应用程序在 8080 上运行,当我输入 localhost:8080 时,应用程序应该重定向到 localhost:8080/swagger-ui.html

我在 application.properties 中添加了以下内容

server.servlet.context-path=/swagger-ui.html

但它不起作用,有人可以帮助我吗

以下是我正在使用的 swagger 依赖项

compile 'io.springfox:springfox-swagger2:2.9.2'
compile 'io.springfox:springfox-swagger-ui:2.9.2'

【问题讨论】:

标签: java spring spring-boot swagger-2.0


【解决方案1】:

您可以像这样在控制器中重定向初始路径“/”:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView method() {
    return new ModelAndView("redirect:" + "/swagger-ui.html");
}

或者您可以在主类中扩展“WebMvcConfigurerAdapter”类并像这样覆盖方法“addViewControllers”:

@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {

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

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

【讨论】:

  • 我试过 registry.addRedirectViewController("/", "/swagger-ui.html");但没有工作
  • 不,我没有
猜你喜欢
  • 2019-12-03
  • 2016-12-24
  • 2021-12-22
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 2016-03-01
  • 1970-01-01
  • 2018-06-08
相关资源
最近更新 更多