19

Spring Boot 2.2 application with springdoc-openapi-ui (Swagger UI) runs HTTP port. The application is deployed to Kubernetes with Ingress routing HTTPS requests from outside the cluster to the service.

In this case Swagger UI available at https://example.com/api/swagger-ui.html has wrong "Generated server url" - http://example.com/api. While it should be https://example.com/api.

While Swagger UI is accessed by HTTPS, the generated server URL still uses HTTP.

Eugene Khyst
  • 9,236
  • 7
  • 38
  • 65

6 Answers6

22

I had same problem. Below worked for me.

@OpenAPIDefinition( 
    servers = {
       @Server(url = "/", description = "Default Server URL")
    }
) 
@SpringBootApplication
public class App {
    // ...
}
t0r0X
  • 4,212
  • 1
  • 38
  • 34
user6070687
  • 221
  • 2
  • 2
  • This worked out-of-the-box for me, thanks! Although, I annotated a `@Configuration` bean/class, not the main application class. I like this solution because 1) it is independent from the Spring Boot backend used (Tomcat, Jetty, Undertow), 2) it doesn't have a hard coded URL, 3) it's a "no code" solution, i.e. has no logic to assemble something at runtime. – t0r0X Sep 08 '22 at 09:55
9

If the accepted solution doesn't work for you then you can always set the url manually by defining a bean.

@Bean
public OpenAPI customOpenAPI() {
    Server server = new Server();
    server.setUrl("https://example.com/api");
    return new OpenAPI().servers(List.of(server));
}

And the url can be defined via a property and injected here.

Peter Lustig
  • 1,585
  • 1
  • 18
  • 34
7

springdoc-openapi FAQ has a section How can I deploy the Doploy springdoc-openapi-ui, behind a reverse proxy?.

The FAQ section can be extended.

Make sure X-Forwarded headers are sent by your proxy (X-Forwarded-For, X-Forwarded-Proto and others).

If you are using Undertow (spring-boot-starter-undertow), set property server.forward-headers-strategy=NATIVE to make a Web server natively handle X-Forwarded headers. Also, consider switching to Undertow if you are not using it.

If you are using Tomcat (spring-boot-starter-tomcat), set property server.forward-headers-strategy=NATIVE and make sure to list IP addresses of all internal proxies to trust in the property server.tomcat.internal-proxies=192\\.168\\.\\d{1,3}\\.\\d{1,3}. By default, IP addresses in 10/8, 192.168/16, 169.254/16 and 127/8 are trusted.

Alternatively, for Tomcat set property server.forward-headers-strategy=FRAMEWORK.

Useful links:

Eugene Khyst
  • 9,236
  • 7
  • 38
  • 65
6

In case you have non-default context path

@Configuration
public class SwaggerConfig {
    
    @Bean
    public OpenAPI openAPI(ServletContext servletContext) {
        Server server = new Server().url(servletContext.getContextPath());
        return new OpenAPI()
                .servers(List.of(server))
                // ...
    }
}
alaster
  • 3,821
  • 3
  • 24
  • 32
1

Make it default to "/" using @Server annotation. This will make the swagger APIs use https

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;

@OpenAPIDefinition(servers = {@Server(url = "/", description = "Default Server URL")})
public class FormService implements ApplicationRunner {
....
}
Raju Reddy
  • 11
  • 3
-1

Below worked for me.

@OpenAPIDefinition(servers = {@server(url = "/", description = "Default Server URL")})
@SpringBootApplication
class App{
// ...
}

or

@OpenAPIDefinition(servers = {@server(url = "/", description = "Default Server URL")})
@Configuration
public class OpenAPIConfig {
@Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("App name")
                        .termsOfService("http://swagger.io/terms/")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")));
    }
}

Generated server url is HHTP - issue

Alisha Raju
  • 726
  • 6
  • 12