使用K8S IP和Nodeport访问时Springboot Swagger Ui抛出Whitelabel错误页面

问题描述 投票:0回答:2

我创建了一个简单的 Springboot 应用程序,它在我的本地主机端口号 9091 上运行,返回“Hello world!!!”当调用 http://localhost:9091/helloWorld url 时。

下面是我的Springboot主类和控制器类的代码片段

@SpringBootApplication
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

}

@RestController
public class HelloWorldController {
    
    @GetMapping(value="/helloWorld")
    public String helloWorld() {
        return "Hello world!!!!";
    }

}

下面是我的 pom.xml 中的依赖项

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

我还可以使用以下 URL http://localhost:9091/swagger-ui/index.html 访问我的 Springboot 项目的 swagger ui

Swagger UI Accessed on Localhost

我已使用以下 yaml 文件配置将此 Springboot 应用程序部署到我的 k8s 集群中

apiVersion: apps/v1
kind: Deployment
metadata:
   name: hello-world
spec:
   replicas: 1
   selector:
      matchLabels:
         app: hello-world
   template:
      metadata:
        labels:
           app: hello-world
      spec:
         containers:
            - name: hello-world
              image: moviepopcorn/hello_world:0.0.1
              ports:
                 - containerPort: 9091
              imagePullPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
   name: hello-world
spec:
   type: NodePort
   selector:
      app: hello-world
   ports:
      - port: 9091
        targetPort: 9091
        nodePort: 30001

我还可以使用以下 url 访问 helloWorld 端点 : 如下所示

Node port service accessed on a browser

但是当我尝试使用以下网址访问 Springboot 的 swagger-ui http://192.168.254.94:30001/swagger-ui/index.html 我看到下面的 Whilelabel 错误页面

Whitelabel Error Page

当我使用集群主节点 IP 访问我的 springboot 应用程序时,您能否帮助我了解导致此 Whitelabel 错误页面问题的原因?

spring-boot kubernetes springdoc-openapi-ui service-node-port-range
2个回答
3
投票

我找到了解决方案。我刚刚在 pom.xml 中添加了以下依赖项

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webflux-ui</artifactId>
        <version>1.6.3</version>
    </dependency>

0
投票

一般来说,只需添加

springdoc-openapi-starter-webmvc-ui
依赖项就可以了。但是,如果您发现从 IntelliJ 内部启动 Spring Boot 时出现Whitelabel 错误页面,请尝试以下操作:

  • 转到文件 > 使缓存无效...,然后单击使缓存无效并重新启动
  • 在终端中,执行
    mvn clean install -DskipTests
  • 重新启动您的应用程序
  • 尝试访问 http://localhost:8080/[context-root]/v3/api-docs 或 http://localhost:8080/[context-root]/swagger-ui/index.html 这对我有用(经过几个小时的谷歌搜索)...
© www.soinside.com 2019 - 2024. All rights reserved.