OpenAPI add server at Server Start

1.3k Views Asked by At

I try to modify server list (or default server) on Swagger UI. 1/ On Spring (Not Spring Boot) configuration class I add this :

    @Bean
public OpenAPI customOpenAPI() {
    log.info("<<<customOpenAPI>>>");
    Server server = new Server();
    server.setUrl("http://localhost:8091/eatery_spring_rs/rs/");
    return new OpenAPI()
            .servers(List.of(server));
}

2/ But I never obtain result on Swagger UI, no server is added : enter image description here

3/ Maven dependencies are :

<!--SWAGGER-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.6.7</version>
        </dependency>
<!-- END -->

5/ Question : Is it possible to change context path, or add server URL, by program ?

1

There are 1 best solutions below

0
On

6/ I can affine the description of problem The context path of application is not complete
It is a Spring 5.3 application, start with web.xml in App Server.
like Tomcat, ...
This is the web.xml definition :

`

<servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet-service.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/rs/*</url-pattern>
</servlet-mapping>`

This mean that the real context path is : [app.context]/rs but in the case of :
@Bean public Docket api(ServletContext servletContext) {

servletContext is equal to [app.context] NOT to [app.context]/rs

It's help ?