0
votes

I am using JAX-RS with Spring Boot and springfox to generate the swagger-ui from annotations.

Currently the docs are located at http://localhost:8080/swagger-ui.html

However, I now have a requirement to move this to http://localhost:8080/api/index.html

Following this issue comment I have a class that looks like this:

@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/");
  }

}

This puts the page at http://localhost:8080/documentation/swagger-ui.html. However, it also still leaves it at http://localhost:8080/swagger-ui.html

How do I change it from swagger-ui.html to index.html?

How do I move it from http://localhost:8080/swagger-ui.html instead of copying it (or just make the original inaccessible)?

If I find/replace the above code for documentation api I have another issue because my servlet is already defined at /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);
  }

}

How can I move swagger-ui to my servlet root instead of my context root?

1

1 Answers

0
votes

I did workaround for you. First I have created Controller with method which will point to index.

@Controller
public class SwaggerUIRedirectController {

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

}

Now create under src/main/resources/templates file index.html and copy content from this file https://github.com/sikynko/swagger_workaround/blob/master/index.html into yours.

And now create under src/main/resources/static/springfox.js and copy content from

https://github.com/sikynko/swagger_workaround/blob/master/springfox.js

into yours. Now when you write http://localhost:8080/index you should see your swagger.

I just copied html from springfox-swagger-ui*.jar swagger-ui.html and in script springfox.js I ahve changed line from

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

on

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

I hope it will help you