我来自c#编程,现在我必须在Spring中创建两个Rest应用程序。一切正常,我可以用springfox- Swagger -ui在swagger中显示API。
但我有两个问题我在网上找不到
谢谢
弹簧引导版本
<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>发布于 2021-12-20 15:01:40
我知道这不是你要问的问题,但是springfox现在对新版本的spring有问题。您使用的spring版本仍然有效,但到2.6时还存在but,而且该项目看起来维护得不太好。由于你是在项目的开始,转换并不是太难。例如,您可以迁移到springdocs (用于迁移:https://springdoc.org/#migrating-from-springfox)。
关于打开一个url,这里提到了一些很好的解决方案:如何使用java打开默认的How浏览器。您可以使您的swagger url属性并让swagger相应地配置它,然后您可以重用该属性在运行时调用url。如果您想要区分不同的环境,我建议使用概要文件。只有当您在dev环境上启动应用程序时,才打开浏览器中的url,然后使用@Profile("dev")指定prod。使用概要注释(runners.htm)创建命令行/应用程序运行程序,并从那里调用url。
话虽如此,结合起来看:
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);
}
}将springdoc.swagger-ui.path=/custom/path放入您的application.properties中,以更改到swagger-ui的路径。
https://stackoverflow.com/questions/70258731
复制相似问题