Idea spring boot项目问题收集
1、创建项目可以参考下面的资料
https://blog.csdn.net/u013248535/article/details/55100979
https://www.cnblogs.com/black-spike/p/8017768.html
2、*** required a bean of type 错误
问题描述:启动类在 package com.example.springboot; 而control,domain等相关的类在 com.example.***其它包,启动时报如题的错误,
解决:在启动类上添加 @ComponentScan(basePackages = {"com.example.domain"}) 意思是启动时还要扫描下这个包下面的类; 由于启动类与其它相关的类不在一个包下,虽然解决了启动问题,但在浏览器打开还是有如下的其它问题,如下:
3、Whitelabel Error Page There was an unexpected error (type=Not Found, status=404)
接上面的问题,终极解决办法是把启动类放在 com.example包,其它的类放在 com.example.***包下面,
在启动类上添加 @ComponentScan(basePackages = {"com.example.*"})
这样启动可以成功,各个control访问也正常了。
4、pom.xml里有的依赖项找不到的解决办法
有时在pom.xml添加了依赖项,maven自动去下载的时候,有时会碰到下载不了,但你本地有这个库的时候,可采用如下办法:
在<dependency>节点中添加如下的项,最后一条为绝对路径,windows下的路径记得 "\"转成"/"
<type>jar</type>
<scope>system</scope>
<systemPath>E:/Java/repo/repo/com/yeepay/g3/yop/yop-sdk/0.0.1/yop-sdk-0.0.1.jar</systemPath>
当然也可以本地配置maven仓库,这里暂不提供这种方法的解决办法
5、To display the conditions report re-run your application with 'debug' enabled.
原因:
spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
解决方法:
在Application类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 或者 配置dataSource
6、Date型字段默认输出是long而不是日期格式问题
对于Date类型字段,如果不适用JsonFormat默认在rest返回的是long,如果我们使用@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm:ss”),就返回"2018-11-16 22:58:15"
如下:
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date regDate;
7、无效的目标发行版: 11
这个问题就是jdk版本设置不对,将运行的jdk版本改成对应本地的版本., 在 setting->build excution deployment->java complier 在右找到项目 更改 target bytecode version
具体参见 无效的目标发行版: 11_Slavic_的博客-CSDN博客_无效的目标发行版
8、springboot 拦截器不起作用的原因
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/**")
.excludePathPatterns("/","/login","/user/login","/index.html","/test");
}
注意:排除哪些不拦截的里面有"/",有很多同学写成"/*",那就不会拦截http://localhost:8080/abc这个url,这是你配置没写对的原因,拦截器里不能配置"/",不然登陆界面都进不去啦,不是这个拦截器不起作用。
9、java.io.NotSerializableException的解决办法
使被序列化的实体类直接实现Serializable接口
10、IDEA中配置spring出现URI is not registered
IDEA中配置spring当web.xml中出现报错,把鼠标放上去出现:
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean就是java对象,由spring创建和管理-->
<bean id="hello" class="com.jie.pojo.Hello">
<property name="str" value="Spring" />
</bean>
</beans>
解决办法:
在 File -> settings -> languages & frameworks 。。。右边点击 +
如图:
备注:此项目非springboot项目, 是spring项目
11、Initialization failed for ‘https://start.spring.io 解决办法
使用IDEA创建SpringBoot项目时,报错: "Initialization failed for 'https://start.spring.io "
方法一 : 通过"File | Settings | Appearance & Behavior | System Settings | HTTP Proxy",选中如图方框内容
点击"check connetion",查看http://start.spring.io是否连接成功
如果现实连接失败,则尝试下一种方法
方法二 测试连接"https://start.aliyun.com/"是否成功
如果测试连接成功,则在创建SpringBoot项目时,选择自定义方式进行创建,输入ali的模板创建SpringBoot项目
12、org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
即在mybatis中dao接口与mapper配置文件在做映射绑定的时候出现问题,简单说,就是接口与xml要么是找不到,要么是找到了却匹配不到。
截图为网络中搜索到的常见原因
还有一个原因就是,mapper接口中定义的方法与 XXXmapper.xml中定义的id是否一致,不一致的话也会存在找不到的问题。
备注:#classpath:只能加载找到的第一个 资源文件classpath*:能加载多 个路径下的资源文件
13、Optional int parameter 'id' is present but cannot be translated into a null value due to being declare
问题出在controller上,原代码如下:
@GetMapping("/city/{id}")
public City ListCityById(int id){
return CityService.SelectCityById(id);
}
改成如下即可
@GetMapping("/city/{id}")
public City ListCityById(@PathVariable("id") int id){
return CityService.SelectCityById(id);
}
--- end ---