springboot+gradle配置过程中遇到的一些小坑和解决记录

从spring转到springboot,配置过程中遇到了一些小问题,虽然都很快的解决了,还是记录一下吧,前车之鉴,以供所需。


1.gradle打包遇到错误

14:39:27.810 [INFO] [org.gradle.api.internal.tasks.compile.JdkJavaCompiler] Compiling with JDK Java compiler API.

14:39:28.033 [ERROR] [system.err] 错误: 无法访问Immutable

14:39:28.033 [ERROR] [system.err]   找不到org.apache.http.annotation.Immutable的类文件

14:39:28.033 [ERROR] [system.err] 1 个错误


原因:

httpclient:4.5.2 is not compatible with httpcore:4.4.5 because class org.apache.http.annotation.Immutable was removed from httpcore:4.4.5 and httpclient:4.5.2 still uses it!

所以,需要在dependency里面加上 4.4.4的httpcore的依赖。

compile("org.apache.httpcomponents:httpclient:4.5.2")
compile("org.apache.httpcomponents:httpcore:4.4.4")
重新编译就好了。

refer:

https://github.com/spring-projects/spring-boot/issues/6662


2.启动springboot application 报错

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

No org.slf4j.impl.StaticLoggerBinder found in ClassPath, trying with log4j2...

No org.apache.logging.log4j.Logger found found in ClassPath, trying with log4j...

No org.apache.log4j.Logger found found in ClassPath, falling back default...

原因是没有import&compile slf4j的log依赖

在dependency中添加

compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('org.slf4j:slf4j-api:1.7.21')
即可。

refer:https://github.com/bonigarcia/webdrivermanager/issues/4


3.解决了上面的问题之后,启动再次报错:

***************************

APPLICATION FAILED TO START

***************************


Description:


Configuration property name 'flowHttpConfig' is not valid:


    Invalid characters: 'H', 'C'

    Bean: payGatewayServiceImpl

    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter


Action:


Modify 'flowHttpConfig' so that it conforms to the canonical names requirements.

这个原因是因为,在springboot升级到2.x.x以后,不允许configuration的property用驼峰的命名方式,要用‘-’分割。

refer:https://github.com/tobato/FastDFS_Client/issues/35


4.Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.main.banner-mode' to org.springframework.boot.Banner$Mode


原因是,yaml/yml中 ‘off’会被转为false,所以需要将配置改成:

spring:
  main:
    banner-mode: 'OFF'
原配置(错误,但是在*.properties文件中没问题):

spring:
  main:
    banner-mode: OFF
refer: https://github.com/spring-projects/spring-boot/issues/4600


5.当使用springboot时,如果依赖中添加了db数据源操作相关的,那么就要配置默认数据源,否则默认加载的时候会报错。

***************************

APPLICATION FAILED TO START

***************************


Description:


Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.


Reason: Failed to determine suitable jdbc url



Action:


Consider the following:

    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.

    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


所以,解决方法:

1.配置默认数据源

2.在需要在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude
= {DataSourceAutoConfiguration.class},排除此类的autoconfig。启动以后就可以正常运行。
这是因为添加了数据库组件,所以autoconfig会去读取数据源配置,而我新建的项目还没有配置数据源,所以会导致异常出现。

refer:https://blog.csdn.net/daxiang52/article/details/79420777


但是,但是,但是,敲黑板!

有同学会发现,添加了exclude之后还是报这个错误,原因是,同时引入了多个db类型的依赖,e.g. 作者同时引入了分布式数据源支持atomikos和mysql的依赖,这时候也是一直报上面的错误。

这时候,去掉一个依赖即可。去掉了atomikos之后,我的启动正常。

refer:https://stackoverflow.com/questions/49475177/failed-to-auto-configure-a-datasource-spring-datasource-url-is-not-specified

冲突的包可能不尽相同,检查方向对了就好。

猜你喜欢

转载自blog.csdn.net/michaelgo/article/details/80861801
今日推荐