1

I come from programming in c# and now I have to create a couple of Rest Apis in Spring Boot. Everything is working ok and I can show the API in Swagger with springfox-swagger-ui

But I have two questions that I could not find in Internet

  1. Is there any way to show the url ui in the console app with server, port, etc?

  2. Is there any way to open the swagger url everytime I run the app in the localhost?

Thanks

Spring boot version

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.5.5</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>

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

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
ssanga
  • 335
  • 3
  • 11
  • 1
    Please, give us more info: Spring-boot and swagger version, p.e. – Jaume Morón i Tarrasa Dec 07 '21 at 10:56
  • 1
    `2. Is there any way to open the swagger url everytime I run the app in the localhost?` - you can customize swagger URL to point to default spring boot url like `localhost:8080/` – ACV Dec 13 '21 at 16:52
  • Open a url n your favorite browser could be possible but close it (like vscode does) when some change is made, not. That's not the purpose of a microservice/api-rest – JRichardsz Dec 17 '21 at 16:35
  • q1 does not make sense to me, since your application might run behind load-balancer or inside kubernetes and why you would be interested in the inner ip oder port? I can understand you, if you want it for development, but then these things are static and there is no need to print it. – Matthias Wiedemann Dec 20 '21 at 09:11

1 Answers1

1

I know it's not the question you're asking, but springfox is currently having issues with newer versions of spring. The spring version you're using is still working but as of 2.6 there are bugs, and it looks like the project is not well maintained. Since you're at the beginning of the project, switching is not too hard. You could move to springdocs for example (for migration: https://springdoc.org/#migrating-from-springfox).

With respect to opening a url, there are some good solutions mentioned here: How to open the default webbrowser using java . You could make your swagger url a property and have swagger configure it accordingly, then you can reuse the property to call the url on run-time. If you want to differentiate between environments I'd suggest use profiles. Only open the url in the browser if you start the app on dev environment, and not on prod is then specified by using @Profile("dev"). Create a commandline/application runner with the profile annotation (https://www.tutorialspoint.com/spring_boot/spring_boot_runners.htm), and call the url from there.

That said, combining it gives:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@Profile("dev")
@Component
public class SwaggerRunner implements ApplicationRunner {

    @Value("${springdoc.swagger-ui.path}")
    private String swaggerPath;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log("\nWelcome to Multi Brow Pop.\nThis aims to popup a browsers in multiple operating systems.\nGood luck!\n");

        final String swaggerUrl = "http://localhost:8000/" + swaggerPath; 
        
        log("We're going to this page: " + swaggerUrl);

        String myOS = System.getProperty("os.name").toLowerCase();
        log("(Your operating system is: " + myOS + ")\n");

        try {
            if (Desktop.isDesktopSupported()) { // Probably Windows
                log(" -- Going with Desktop.browse ...");
                Desktop desktop = Desktop.getDesktop();
                desktop.browse(new URI(swaggerUrl));
            } else { // Definitely Non-windows
                Runtime runtime = Runtime.getRuntime();
                if (myOS.contains("mac")) { // Apples
                    log(" -- Going on Apple with 'open'...");
                    runtime.exec("open " + swaggerUrl);
                } else if (myOS.contains("nix") || myOS.contains("nux")) { // Linux flavours
                    log(" -- Going on Linux with 'xdg-open'...");
                    runtime.exec("xdg-open " + swaggerUrl);
                } else
                    log("I was unable/unwilling to launch a browser in your OS :( #SadFace");
            }
            log("\nThings have finished.\nI hope you're OK.");
        } catch (IOException | URISyntaxException eek) {
            log("**Stuff wrongly: " + eek.getMessage());
        }
    }

    private static void log(String log) {
        System.out.println(log);
    }

}

put springdoc.swagger-ui.path=/custom/path in your application.properties to change the path to your swagger-ui

BarbetNL
  • 408
  • 2
  • 16