当前位置: 首页 > 知识库问答 >
问题:

Spring云网关;在classpath上发现Spring MVC,与Spring Cloud Gateway问题不兼容

万俟鸿波
2023-03-14

我在运行API-GATEWAY时出现了以下错误,我尝试了很多方法,但我无法解决这个问题。

说明:

package com.sample.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayApplication {

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

}
spring:
  application:
    name: GATEWAY-SERVICE

  cloud:
    gateway:
      routes:
        - id: USER-SERVICE
          uri: lb://USER-SERVICE
          predicates:
            - Path=/users/**
        - id: DEPARTMENT-SERVICE
          uri: lb://DEPARTMENT-SERVICE
          predicates:
            - Path=/departments/**

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/

  instance:
    hostname: localhost

server:
  port: 9191

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sample.apigateway</groupId>
    <artifactId>apigateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apigateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

完整错误信息

2021-07-30 23:12:02.420  WARN 19032 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration$SpringMvcFoundOnClasspathConfiguration': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration$SpringMvcFoundOnClasspathConfiguration]: Constructor threw exception; nested exception is org.springframework.cloud.gateway.support.MvcFoundOnClasspathException
2021-07-30 23:12:02.423  INFO 19032 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-07-30 23:12:02.441  INFO 19032 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-30 23:12:02.454 ERROR 19032 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.

Action:

Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.


Process finished with exit code 1

共有1个答案

柳俊健
2023-03-14

我也面临着类似的问题。正如错误部分所建议的,对application.properties文件进行以下更改对我来说很有效。

spring.main.web-application-type=reactive
 类似资料:
  • 我试图为一个测试项目设置spring cloud gateway,但当我使用“lb://”URL时,它总是失败。

  • 我还需要NGINX来服务静态内容,如JS等,并向后端反向代理请求,还是可以用Spring Cloud Gateway来完成?Spring docs有以下图像: 我没有发现关于如何将静态内容返回给客户端的描述,这是否意味着它被认为是糟糕的做法,我需要额外的反向代理步骤来增加它的延迟?如果没有,我可以在哪里找到更多关于如何使用Spring Cloud Gateway实现这一点的信息,尤其是如果我要使用

  • 我一直使用Zuul作为边缘服务和API网关。最近我注意到Spring云平台发布了Spring云网关。这两个网关有什么区别?为什么Zuul没有扩展到支持S-C-Gateway中的功能?一个新图书馆的驱动因素是什么?应该在什么时候使用?

  • 我正在尝试将spring云网关与kubernetes服务发现结合使用。下面是我正在使用的设置 建筑格拉德尔 application.yml 演示应用程序。Java语言 Spring云网关无法将请求重定向到其他服务。正在打印的日志是 2019-10-13 18:29:38.303TRACE 1---[or-超文本传输协议-epolp-2]o. s. c. g. f. Weght计算器WebFilte

  • 我已经试着解决这个问题好几天了,我在论坛上也找不到任何有类似问题的帖子。当我将servlet映射url模式设置为*时,Spring主题运行良好。web中的html。xml文件。但是,如果我更改web中的url模式。将xml文件改为正斜杠,以便默认servlet处理所有请求,并且URL可以看起来像REST URL,然后我使用它来获得无映射错误,这导致无法显示网页的css设置。我用来获取的无映射警告如