spring boot常见错误
- url失败
- 端口占用
- 利用maven对项目进行打包报错
- 各种Error creating bean with name
- AyUser实体类所存在的问题
- Error:java: 无效的目标发行版: 11
- 初始化时没有选择依赖怎么办
- PageRequest形参和实参数量不匹配
- 无限重定向
- 监听器与redis缓存结合错误
- findOne(id)报错
- 日志jar包冲突
- 后续补充
本文章只针对某高校web课程,可能无法解决你的问题
如果spring boot程序运行错误可以先debug查看下错误所在
url失败
解决方法:在application.properties文件中添加spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test,端口自定义
端口占用
解决方法:SpringBoot的默认端口是8080,那现在我想要让它的端口不要8080,而是8081,或者说另一个项目占用了8080,需要我们把现在的项目改成8081,那么我们就可以在application.properties文件中添加
server.port=8081
利用maven对项目进行打包报错
解决方法:
在要打包的加多这两句成功打包
各种Error creating bean with name
解决方法
1、将application与各文件夹并列摆放
AyUser实体类所存在的问题
1、@Table(name=""),双引号内要写自己数据库内的表名
2、一定要加@Entity
3、@Id import引用的是import javax.persistence.Id,而不是
4、爆红
解决方法:将鼠标放置在Id上,输入alt+enter,选择
Error:java: 无效的目标发行版: 11
初始化时没有选择依赖怎么办
解决方法:直接在pom.xml cv上想要的依赖就行,然后再刷新maven
刷新maven:
PageRequest形参和实参数量不匹配
版本号为2016.2才不会报错
高版本解决方法:
改成Pageable pageable = PageRequest.of(0,10);即可
无限重定向
将红框内的com改为cn即可解决
监听器与redis缓存结合错误
运行springboot项目出现java.lang.NullPointerException: null
很明显,就是没有对象了(哭了)
原因就是Lister没有读取到这两个类声明
那没办法了,我只好自己在自己的方法里实现了(没有对象我们自己就想象一个)
具体代码:
监听器初始化代码
public void contextInitialized(ServletContextEvent servletContextEvent) {
AyUserService ayUserService = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext()).getBean(AyUserService.class);
ayUserService.pushAyUserToRedis(ALL_USER);
List<AyUser> queryUserList = ayUserService.queryAllAyUserFromRedis(ALL_USER);
System.out.println("缓存中目前的用户数有:" + queryUserList.size() + " 人");
System.out.println("ServletContext上下文初始化");
// System.out.println("ServletContext上下文初始化");
// //查询数据库所有的用户
// List<AyUser> userList = ayUserService.findAll();
// //清除缓存中的用户数据
// redisTemplate.delete(ALL_USER);
// //将数据存放到redis缓存中
// redisTemplate.opsForList().leftPushAll(ALL_USER, userList);
// //真实项目中需要注释掉,查询所有的用户数据
// List<AyUser> queryUserList = redisTemplate.opsForList().range(ALL_USER, 0, -1);
// System.out.println("缓存中目前的用户数有:" + queryUserList.size() + " 人");
}
接口方法定义:
接口方法实现
代码:
@Override
public void pushAyUserToRedis(String allUser){
List<AyUser> userList = ayUserService.findAll();
//清除缓存中的用户数据
redisTemplate.delete(allUser);
//将数据存放到redis缓存中
redisTemplate.opsForList().leftPushAll(allUser, userList);
}
@Override
public List<AyUser> queryAllAyUserFromRedis(String allUser){
List<AyUser> queryUserList = redisTemplate.opsForList().range(allUser, 0, -1);
return queryUserList;
}
添加类声明
findOne(id)报错
Inferred type ‘S’ for type parameter ‘S’ is not within its bound; should extends xxxxxx
springboot 版本问题,将 2.0.1 版本换成 1.5.4 版本。
将girlRepository.findOne(id); 改为 girlRepository.findById(id).orElse(null); 或 girlRepository.findById(id).get();
日志jar包冲突
springBoot框架集成log4j2框架时,使用idea开发工具,修改pom.xml文件如下:
<!--添加对SpringMVC和Tomcat的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions> <!--去除之前对老版本log4j的依赖-->
<exclusion>
<artifactId>org.springframework.boot</artifactId>
<groupId>spring.boot.starter-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!--添加对日志系统的支持,采用最新的log4j2框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
运行程序出现如下错误:
Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:13 - no applicable action for [appenders], current ElementPath is [configuration]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:47 - no applicable action for [Console], current ElementPath is [configuration[Console]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@8:70 - no applicable action for [ThresholdFilter], current ElementPath is [configurationConsole]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@10:86 - no applicable action for [PatternLayout], current ElementPath is [configurationConsole]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@14:59 - no applicable action for [File], current ElementPath is [configuration[File]]
错误原因:
引用jar时引用了多个logback的框架,由于idea开发工具未根据pom.xml的配置自动配置依赖,未去除之前的log4j的依赖,导致无法解析节点信息。
解决办法:
在pom.xml文件中右击选择Diagrams,查看依赖图,找到关于logging的依赖,删除即可。
后续补充
如果大家有相关问题解决的,也可以私信我,我一起补充在这里,帮助大家解惑