Spring Boot异常整理
0
这里是一个Spring Boot的示例,里面包含了一些JPA、C3P0连接池、AOP、拦截器、Freemarker、缓存的配置。
地址:https://gitee.com/acgist/demo/tree/master/springboot
后面是一些遇到的错误整理。
Spring Boot和Java9,启动时出现以下错误:
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
这个是因为Java9模块化,所以以前JavaEE的一些包放到了java.se.ee
模块,默认导入模块java.se
是不包含以前的一些功能了。
所以需要启动参数添加--add-modules java.xml.bind
或者修改JDK为9以前的版本。
添加参数如图:
也可以配置--add-modules=java.xml.bind
也可以,中间的等号可有可无,如果多个模块中间用逗号分隔
--add-modules jdk.incubator.httpclient,java.xml.bind
--add-modules=jdk.incubator.httpclient,java.xml.bind
如果实在Tomcat中使用,需要在启动脚本中添加:
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-modules java.xml.bind"
添加位置如图:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.acgist.dao.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Description:
Field userRepository in com.acgist.service.UserService required a bean of type 'com.acgist.dao.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.acgist.dao.UserRepository' in your configuration.
Repository接口注入失败,查看日志发现:
Ignored because not a concrete top-level class: file [E:\git\spring\springboot\target\classes\com\acgist\dao\UserRepository.class]
添加注解@EnableJpaRepositories("com.acgist.dao")
,不然会忽略没有实现的接口。
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.acgist.entity.UserEntity
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:472)
添加实体包扫描注解@EntityScan("com.acgist.entity")
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project acgist_gx: Compilation failure: Compilation failure:
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[13,21] 程序包 jdk.incubator.http 不可见
[ERROR] (程序包 jdk.incubator.http 已在模块 jdk.incubator.httpclient 中声明, 但该模块不在模块图中)
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[14,21] 程序包 jdk.incubator.http 不可见
[ERROR] (程序包 jdk.incubator.http 已在模块 jdk.incubator.httpclient 中声明, 但该模块不在模块图中)
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[15,38] 程序包jdk.incubator.http.HttpRequest不存在
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[16,38] 程序包jdk.incubator.http.HttpRequest不存在
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[17,21] 程序包 jdk.incubator.http 不可见
[ERROR] (程序包 jdk.incubator.http 已在模块 jdk.incubator.httpclient 中声明, 但该模块不在模块图中)
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[59,17] 找不到符号
[ERROR] 符号: 类 Builder
[ERROR] 位置: 类 com.acgist.modules.http.HTTPTools
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[69,17] 找不到符号
[ERROR] 符号: 类 BodyPublisher
[ERROR] 位置: 类 com.acgist.modules.http.HTTPTools
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[41,17] 找不到符号
[ERROR] 符号: 类 Builder
[ERROR] 位置: 类 com.acgist.modules.http.HTTPTools
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[54,17] 找不到符号
[ERROR] 符号: 类 Builder
[ERROR] 位置: 类 com.acgist.modules.http.HTTPTools
[ERROR] /E:/GITEE/acgist_gx/src/main/java/com/acgist/modules/http/HTTPTools.java:[60,17] 找不到符号
[ERROR] 符号: 类 Builder
[ERROR] 位置: 类 com.acgist.modules.http.HTTPTools
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
Maven打包时提示上面的错误,和上面的一样需要添加模块jdk.incubator.httpclient
,添加下面代码到pom.xml
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules</arg>
<arg>jdk.incubator.httpclient</arg>
</compilerArgs>
</configuration>
</plugin>
运行时提示:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: jdk/incubator/http/HttpRequest
需要需要添加参数:
java -jar --add-modules jdk.incubator.httpclient app.jar
单元测试找不到配置错误提示:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/email.properties]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
配置@PropertySource(value = "/email.properties")
改为@PropertySource(value = "classpath:/email.properties")