如何使用springfox和JAX-RS重定位swagger-ui.html?

时间:2018-10-20 05:26:43

标签: spring-boot jax-rs springfox

我正在将JAX-RS与Spring Boot和springfox结合使用,以根据注释生成swagger-ui。

当前文档位于http://localhost:8080/swagger-ui.html

但是,我现在需要将其移至http://localhost:8080/api/index.html

this issue comment之后,我有一个看起来像这样的课:

@Configuration
public class SwaggerUIConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/documentation/v2/api-docs", "/v2/api-docs");
    registry.addRedirectViewController("/documentation/configuration/ui", "/configuration/ui");
    registry.addRedirectViewController("/documentation/configuration/security", "/configuration/security");
    registry.addRedirectViewController("/documentation/swagger-resources", "/swagger-resources");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
      .addResourceHandler("/documentation/**")
      .addResourceLocations("classpath:/META-INF/resources/");
  }

}

这会将页面置于http://localhost:8080/documentation/swagger-ui.html。但是,它仍然保留在http://localhost:8080/swagger-ui.html

如何将其从swagger-ui.html更改为index.html

如何将其从http://localhost:8080/swagger-ui.html中移出而不是复制(或仅使原始文件无法访问)?

如果我找到/替换了documentation api的上述代码,则会遇到另一个问题,因为我的servlet已在/ api中定义。

@SpringBootApplication
@EnableSwagger2
public class SpringApplication extends SpringBootServletInitializer {

  @Bean
  public ServletRegistrationBean api() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(),"/api/*");
    registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, SwaggerResourceConfig.class.getName());
    registration.setName("api");
    return registration;
  }
}

public class SwaggerResourceConfig extends ResourceConfig {

  public SwaggerResourceConfig() {
    register(ApiListingResource.class);
    register(SwaggerSerializers.class);
  }

}

如何将swagger-ui移到我的servlet根目录而不是上下文根目录?

1 个答案:

答案 0 :(得分:0)

我为您解决了问题。首先,我创建了带有方法的Controller,该方法将指向索引。

@Controller
public class SwaggerUIRedirectController {

    @RequestMapping("/index")
    public String uiRedirectLink() {
        return "index";
    }

}

现在在src / main / resources / templates文件下创建index.html文件,并从该文件复制内容 multipoly.v 进入你的。

现在在src / main / resources / static / springfox.js下创建 并从

复制内容

https://github.com/sikynko/swagger_workaround/blob/master/index.html

加入您的。 现在,当您写https://github.com/sikynko/swagger_workaround/blob/master/springfox.js时,应该会看到您的招摇。

我只是从springfox-swagger-ui * .jar swagger-ui.html复制了html,并且在脚本springfox.js中,我从

更改了行
var urlMatches = /(.*)\/swagger-ui.html.*/.exec(window.location.href);

var urlMatches = /(.*)\/(.*).*/.exec(window.location.href);

我希望它将对您有帮助