All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.luohuasheng.bee.rest.swagger.config.SwaggerConfig Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package net.luohuasheng.bee.rest.swagger.config;


import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import net.luohuasheng.bee.rest.swagger.properties.SwaggerProperties;
import net.luohuasheng.bee.rest.swagger.utils.LocalIpAddressUtils;
import org.springdoc.core.SwaggerUiConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;

/**
 * @author luohuasheng
 * @date 2020/7/28 08:26
 */
@Configuration
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfig implements ApplicationListener {
    private static final int SPLIT_SIZE = 2;

    private final SwaggerProperties swaggerProperties;

    private int serverPort;
    @Value("${server.servlet.context-path:}")
    private String path;
    @Autowired(required = false)
    private SwaggerUiConfigProperties swaggerUiConfigProperties;


    public SwaggerConfig(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Bean
    public OpenAPI springShopOpenApi() {
        return new OpenAPI()
                .info(new Info().title(swaggerProperties.getTitle())
                        .description(swaggerProperties.getDescription())
                        .version(swaggerProperties.getVersion()))
                ;
    }

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        if (swaggerUiConfigProperties == null) {
            return;
        }
        this.serverPort = event.getWebServer().getPort();
        printSwaggerPath();
    }

    private void printSwaggerPath() {
        System.out.println("////////////////////////////////////////////////////////////////////");
        printMessage("");
        printMessage("Swagger URL");
        printMessage("");

        for (String localIp : LocalIpAddressUtils.getLocalIps()) {
            printMessage(("http://" + localIp + ":" + serverPort + path + "/" + "swagger-ui.html"));
        }
        printMessage("");
        printMessage("");
        printMessage("");
        System.out.println("////////////////////////////////////////////////////////////////////");

    }

    private void printMessage(String messgae) {
        int size = 64 - messgae.length() - (messgae.getBytes(StandardCharsets.UTF_8).length - messgae.length()) / 2;
        int left = size / SPLIT_SIZE, right;
        if (size % SPLIT_SIZE == 0) {
            right = left;
        } else {
            right = left + 1;
        }
        System.out.println("//" + space(left) + messgae + space(right) + "//");
    }

    private String space(int left) {
        StringBuilder sb = new StringBuilder();
        for (int i = left; i > 0; i--) {
            sb.append(" ");
        }
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy