Spring – the boot – start – web dependency

Start the error

The first time the gateway project is started, the error message is as follows:

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

Description:

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.
Copy the code

Error analysis

The ServerCodecConfig Bean cannot be found in GatewayAutoConfiguration.

Gateway is a spring project boot program, to load it at start-up time configuration, one of them called GatewayClassPathWarningAutoConfiguration configuration classes have so one line of code:

@Configuration
@ConditionalOnClass(name = "org.springframework.web.servlet.DispatcherServlet")
protected static class SpringMvcFoundOnClasspathConfiguration {

    public SpringMvcFoundOnClasspathConfiguration(a) {
        log.warn(BORDER+"Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. " + "Please remove spring-boot-starter-web dependency."+BORDER); }}Copy the code

Spring MVC is found on the classpath and is incompatible with the Spring Cloud Gateway at this time. Please remove the spring-boot-start-web dependency.” .

The solution

Exclude spring-boot-starter- Web dependencies.

I started it again, and it worked. O ha ha ~ O (studying studying)

The spring – the boot – start – web dependency

An error occurs when the database dependencies are added

Yesterday, when integrating OAuth2.0, the following dependencies were added because of the need to save tokens to the database:

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<! -- druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>
Copy the code

Error:

05-06 11:57:08.590 ERROR o.s.boot.SpringApplication [826] Application run failed
java.lang.NoClassDefFoundError: javax/servlet/Servlet
	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
	at java.lang.Class.getDeclaredConstructors(Class.java:2020) at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.findConstructorBindingAnnotat edConstructor(ConfigurationPropertiesBindConstructorProvider.java:62) at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.getBindConstructor(Configurat ionPropertiesBindConstructorProvider.java:48) at org.springframework.boot.context.properties.ConfigurationPropertiesBean$BindMethod.forType(ConfigurationPropertiesBean.j ava:311) at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropert iesBeanDefinitionValidator.java:63) at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(Config urationPropertiesBeanDefinitionValidator.java:45) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegis trationDelegate.java:286) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegis trationDelegate.java:174) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContex t.java:706)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationCo ntext.java:66)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at com.ileadtek.gateway.CpGatewayApplication.main(CpGatewayApplication.java:18)
Caused by: java.lang.ClassNotFoundException: javax.servlet.Servlet
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)...19 common frames omitted
Copy the code

Discovered because Druid relies on javax.servlet-API

But after I add the servlet dependency, I get an error:

We added tomcat-embed-core dependencies to resolve this problem, but when we started again, we found that we had the same jar conflicts as before:

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

Description:

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.
Copy the code

Ok, it will work without the spring-boot-starter web dependency ( ̄▽ ̄)/but… I can’t find any projects that rely on the spring-boot-starter web.

Is it! The problem of Druid

The main reason is that it indirectly relies on Tomcat-embed core, so we had to change the data source:

I started it again, and it worked. O ha ha ~ O (studying studying)

conclusion

It seems that the spring-cloud-starter-gateway is not the reason of the Spring-boot-starter -web. I found that the Spring-boot-starter -web also relies on the Tomcat-embed -core.

So I think the fundamental thing is the Tomcat-embed core dependency package (* ̄) in the project.

reference

www.codeleading.com/article/651…

If you also encounter this problem, welcome to communicate with oh (#^ ^ ^#)