spring-bootswaggernetflix-eurekacontextpath

Add context path to requests in swagger


I have an eureka service which has a swagger. The eureka is on http://localhost:8050 and the service goes by name /service. The issue is that when i open swagger and try to make a request, it sends it to http://localhost:8050/service/somecontroller. The service has a context path "path" so it should be http://localhost:8050/service/path/somecontroller. This is the configuration of the swagger:

@Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select()
            .apis(RequestHandlerSelectors.basePackage("com.test")).paths(PathSelectors.any())
            .build();
    }

Solution

  • Managed to change the context path of the swagger like this:

    @Value("${contextPath}")
    private String contextPath;
    
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            //.host(retrieveHostHostname())
            .pathProvider(new PathProvider() {
            @Override
            public String getApplicationBasePath() {
                return contextPath;
            }
    
            @Override
            public String getOperationPath(String s) {
                return s.replace("somecontroller", contextPath+"/somecontroller");
            }
    
            @Override
            public String getResourceListingPath(String s, String s1) {
                return "/";
            }
        }).select()
            .apis(RequestHandlerSelectors.basePackage("com.test")).paths(PathSelectors.any())
            .build();
    }