Relaxx
Relaxx

Reputation: 1

Provide swagger ui behind reverse proxy (without application.properties for base context)

i have a software which has two different tasks:

  1. Lets say an application task
  2. A rest api task

The application task on base context {host}/application/... is initialized with Springboot as follows:

    public static void main(String[] args) {
        new SpringApplicationBuilder(ApplicationStarter.class).properties(Properties.loadPortProperty()).run(args);
    }

    @Bean
    public ServletRegistrationBean endpoint() {
        return new ServletRegistrationBean(new ApplicationServlet(), "/application/*");
    }

The rest api task which contains swagger is configured as follows (the rest api endpoints are not required at this point i think):

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
@Controller
@RequestMapping("/api")
public class HomeController {

    @ApiIgnore
    @GetMapping(value = "")
    public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/api/swagger-ui.html");
    }

}
@Configuration
@EnableSwagger2
public class SwaggerDocumentationConfig implements WebMvcConfigurer {

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("test.package"))
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/api/swagger-ui.html**")
                .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/api/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addRedirectViewController("/api/v2/api-docs", "/v2/api-docs");
        registry.addRedirectViewController("/api/swagger-resources/configuration/security", "/swagger-resources/configuration/security");
        registry.addRedirectViewController("/api/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui");
        registry.addRedirectViewController("/api/swagger-resources", "/swagger-resources");
    }

}

So this configuration runs fine on my local machine. The problem is if this configuration runs behind a reverse proxy i can't reach the swagger-ui.html because of the redirects on swagger without /api/ reaches a whole different deployment (e.g. another web ui). In result the browser shows for /api/swagger-ui.html the http error 404 respectively an Unable to infer base url message.

If i set the /api/ via the application.properties file below the project resources the problem is that the first task (the application task) changes its base context to /api/application/... which is not wanted... but if i will do it the swagger ui works as intended.

So my question is how to tell the whole swagger deployment that it should use the base context path /api/ and not / ?

Thank you for your support and i hope it is understandable what the problem is :)

Upvotes: 0

Views: 1397

Answers (1)

Relaxx
Relaxx

Reputation: 1

i found a solution by myself and would post it for others which could have the same issue. I simply provide my own endpoint implementations for

  • /api/swagger-resources/configuration/security
  • /api/swagger-resources/configuration/ui
  • /api/swagger-resources

and returning manually the values which are required for the swagger-ui.html... Considering that these JSON values are static there should no problem to do that workarount.

Upvotes: 0

Related Questions