Search code examples
springswaggerswagger-uispringfox

SpringFox Swagger UI has wrong base url


I got confused with spring fox swagger ui base url, they are not pointing to correct url.

I just deployed a war in a context, so the app is in 127.0.0.1:8080/bff, i managed to add swagger and success, now its running in 127.0.0.1:8080/bff/swagger-ui.html, but when i try to test the api its pointing to 127.0.0.1:8080/bff/v2/api-docs/api/v1/home/profile. Why there is v2/api-docs !?

I know the API list on the swagger-ui is populated from that one, but why it's injected to URL when we test the API? because all of my API lay on the 127.0.0.1:8080/bff/api/v1

This is the screenshot enter image description here

This is the code.

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private GitVersionPropertiesConfig gitVersionPropertiesConfig;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(
                        Lists.newArrayList(new ParameterBuilder()
                                .name("Authorization")
                                .description("OAUTH2 Token")
                                .modelRef(new ModelRef("string"))
                                .parameterType("header")
                                .required(false)
                                .build()))
                .apiInfo(apiInfo())
                .pathMapping("/")
                .pathProvider(new RelativePathProvider(null) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/bff/";
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.regex("/api.*"))
                .build();
    }

    ApiInfo apiInfo() {
        String desc = "Bima Friends Forever API<br>"
                + "Current Branch    : <b>"+gitVersionPropertiesConfig.getGitBranch()+"</b><br>"
                + "Timestamp         : <b>"+gitVersionPropertiesConfig.getGitBuildTime()+"</b>";
        return new ApiInfoBuilder()
                .title("BFF - Hutchison")
                .description(desc)
                .version(gitVersionPropertiesConfig.getGitCommitIdAbbrev())
                .build();
    }
}

This is the temporary fix, but not permanent.

Open browser console and run window.swaggerUi.api.setBasePath('/bff');

Server : Wildfly Swagger UI Version : 2.7.0

Thanks in advance.


Solution

  • I manage to fix it.. the culprit was jboss-web.xml context

    Previously

    <jboss-web>
        <context-root>/bff/</context-root>
    </jboss-web>
    

    Fix :

    <jboss-web>
        <context-root>/bff</context-root>
    </jboss-web>
    

    oh my god...