3

I am using spring boot 2.2.4 to build a REST API, and springfow 2.9 for api doc.

I deployed the app in a docker container, and I am able to access the swagger-ui.htmlpage. However, when using "ty it out" feature, the request is fired on the localhost url (https://locahost/api/find), instead of the service url (https://service.acces.url/api/find). How can I fix that ?

swagger configuration:

    @Bean
public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("fr.test")) // only select API in this package
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            ;
}

// build some user friendly name and description for the API doc UI
private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
            .title("test")
            .description("test.")
            .license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
            .build()
            ;
}

EDIT
I can see that the link to /v2/api-docs si well set, but couldn't find how they did in the source code...

enter image description here

Ulky Igor
  • 322
  • 5
  • 16
  • possible dupe of https://stackoverflow.com/questions/36198827/how-to-change-basepath-for-springfox-swagger-2-0 – MarkHu Jul 08 '21 at 02:26

1 Answers1

1

By default, when you use the "Try it out" feature on Swagger UI, it would make an HTTP call on localhost itself. In case you want to override that behaviour, you need to set the host property of Docket with the required hostname as below:

@Bean
public Docket api(){
    return new Docket(DocumentationType.SWAGGER_2)
            .host("hostname")
            .select()
            .apis(RequestHandlerSelectors.basePackage("fr.test")) // only select API in this package
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo())
            ;
}

// build some user friendly name and description for the API doc UI
private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
            .title("test")
            .description("test.")
            .license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
            .build()
            ;
}

It would be ideal to get that hostname value from a configuration so that you can modify it when required without changing your code. And when you set it as service.acces.url as you require, the HTTP call on Swagger UI would be made to https://service.acces.url/api/find.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54
  • @UlkyIgor did this help? – Madhu Bhat Mar 30 '20 at 15:04
  • sorry, got lost in my work. Thank you for the answer. I already knew I could change the "host" in the API, but it will be difficult since the app service url is something that can change depending on the deployment environment (QA, PROD, ETC..). Furthermore, the app is deployed in a docker container and registered to an API gateway. Way too many params to take into account if I want to set the hostname manualy. I see on the swagger ui that the link to* /v2/api-docs* (just bellow baseUrl) is well set, but I couldn't find in their source code how they did that... – Ulky Igor Mar 31 '20 at 10:10
  • UP.. still strugling on this :/ – Ulky Igor Jun 05 '20 at 09:13
  • Setting the `.host()` can be used to kludge the proper path, but `.pathMapping()` works a little better. What we really need is an easy way to set Swagger's basePath parameter. --perhaps https://stackoverflow.com/questions/36198827/how-to-change-basepath-for-springfox-swagger-2-0 – MarkHu Jul 08 '21 at 02:25