[Solved] Spring Cloud Gateway Startup Error: Parameter 0 of method modifyRequestBodyGatewayFilterFactory in…

Error message:

Spring Cloud Gateway startup has been reporting an error with detailed error information

Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer’ that could not be found.

Action:

Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecConfigurer’ in your configuration.

View the source code:

1、GatewayClassPathWarningAutoConfiguration.java

The spring cloud Gateway gateway itself is a Spring Boot project. When the gateway microservice starts, it will automatically load its configuration. Among them, there is a configuration class called GatewayClassPathWarningAutoConfiguration , as follows:

You will see @ConditionalOnMissingClass({“org.springframework.web.reactive.DispatcherHandler”})

Add spring-boot-starter-webflux dependency if there is no DispatcherHandler.

Remove the spring-boot-starter-web dependency if there is a DispatcherHandler.

@Configuration(
    proxyBeanMethods = false
)
@AutoConfigureBefore({GatewayAutoConfiguration.class})
public class GatewayClassPathWarningAutoConfiguration {
    private static final Log log = LogFactory.getLog(GatewayClassPathWarningAutoConfiguration.class);
    private static final String BORDER = "\n\n**********************************************************\n\n";

    public GatewayClassPathWarningAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnMissingClass({"org.springframework.web.reactive.DispatcherHandler"}) //重点注解
    protected static class WebfluxMissingFromClasspathConfiguration {
        public WebfluxMissingFromClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring Webflux is missing from the classpath, which is required for Spring Cloud Gateway at this time. Please add spring-boot-starter-webflux dependency.\n\n**********************************************************\n\n");
        }
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass(
        name = {"org.springframework.web.servlet.DispatcherServlet"}
    )
    protected static class SpringMvcFoundOnClasspathConfiguration {
        public SpringMvcFoundOnClasspathConfiguration() {
            GatewayClassPathWarningAutoConfiguration.log.warn("\n\n**********************************************************\n\nSpring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.\n\n**********************************************************\n\n");
        }
    }
}

If the spring-boot-starter-web dependency is added to the pom file, the org.springframework.web.servlet.DispatcherServlet filter will exist in the project, which is incompatible with Spring Cloud Gateway conflicts.

2、GatewayAutoConfiguration.java

Open the GatewayAutoConfiguration class in the gateway automatic configuration class, and see that the bean org.springframework.http.codec.ServerCodecConfigurer is required in the following methods

@Bean
public ModifyRequestBodyGatewayFilterFactory modifyRequestBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer) {
    return new ModifyRequestBodyGatewayFilterFactory(codecConfigurer.getReaders());
}

@Bean
public DedupeResponseHeaderGatewayFilterFactory dedupeResponseHeaderGatewayFilterFactory() {
    return new DedupeResponseHeaderGatewayFilterFactory();
}

@Bean
public ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer, Set<MessageBodyDecoder> bodyDecoders, Set<MessageBodyEncoder> bodyEncoders) {
    return new ModifyResponseBodyGatewayFilterFactory(codecConfigurer.getReaders(), bodyDecoders, bodyEncoders);
}

Solution:

Spring cloud gateway is based on webflux, so remove the spring-boot-start-web dependency directly. If you need web services, add the spring-boot-starter-webflux dependency.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *