Harri Eleftheriadis
Harri Eleftheriadis

Reputation: 113

Swagger ui using ip and port instead of domain name

I am running a sprinboot application using swagger. I just followed a tutorial and in principal it works locally just by including springdoc in my pom.xml.

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.5</version>
    <!--<version>1.2.32</version>-->                                                                                                                                                                                 
</dependency>

I also changed the default swagger ui path in the application.properties file

springdoc.swagger-ui.path=/harri-api.html

No further configuration done.

I deployed my application on a server (running on port 7879) on which the swagger ui is available by its domain - harri.de/harri-api.html. But when I want to use my REST-API itself, swagger does not use the domain name harri.de but the servers ip and port.

Maybe someone can give me a hint where and how to configure the behavior of swagger ui. I just want swagger to use the domain name instead of ip:port

Many thanks in advance Harri E.

Upvotes: 4

Views: 4585

Answers (2)

Jesus Redon
Jesus Redon

Reputation: 41

Just enable on Nginx the the Host name:

location /openapi/ {
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_pass http://localhost:8081/openapi/;
}

Upvotes: 3

Harri Eleftheriadis
Harri Eleftheriadis

Reputation: 113

I managed to implement a acceptable solution for my problem. I added OpenAPi configuration as annotation in my main class. To be more specific, I defined two servers which are than selectable in the swagger ui editor.

@OpenAPIDefinition(servers = {@Server(url = "http://127.0.0.1:7879"), @Server(url = "https://harri.de/")})
@SpringBootApplication
public class HarriApplication {

    public static void main(String[] args) {
        SpringApplication.run(HarriApplication.class, args);
    }
    ...
}

The two server are now available in the top dropdown of swagger ui.

Remark

That solution works for me but has basic disadvantages. The configuration is done hard coded inside my sources. This is not very flexible. When deploying on a different host, this approach needs rebuilding of the application.

I also tried to provide a yaml configuration file to add the server entries. But that approach did not work. Referencing a yaml config file like:

servers: 
  - url: https://harri.de
  - url: http://127.0.0.1:7879

Suppresses the generation of my API spec from soource. It does not behave like a extension on the generated specification.

Upvotes: 0

Related Questions