一、小白坑
1 页面404
1. 确认输入项目路径是否正确,如:http://localhost:8080/index,这里需要注意的是端口号的查看
2. 确认注解是否用对
在 Controller 层类上面使用的注解是 @RestController 而并非是 @Controller,或者是 @Controller + @ResponseBody;
详解:如果返回 String 或者 json 的话就直接类上用 @RestController;
如果想要页面跳转的话,就使用 @Controller;
如果只有在某方法上返回 json,其他方法跳转页面,则在类上添加 @Controller,在需要返回 String 和 json 的方法上添加 @ResponseBody 注解;
3. 确认导入项目注解的包地址是否正确
@RequestMapping("/index")
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
import org.springframework.web.bind.annotation.RestController;
4. 启动类位置错误
spring-boot会自动加载启动类所在包下及其子包下的所有组件,且启动类不能直接放在main下,要放在包里
2 @ConfigurationProperties注解松散绑定错误
项目启动时报错,bean初始化失败
***************************
APPLICATION FAILED TO START
***************************
Description:
Configuration property name 'foodProperties' is not valid:
Invalid characters: 'P'
Bean: foodProperties
Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter
Action:
Modify 'foodProperties' so that it conforms to the canonical names requirements.
Process finished with exit code 1
Component 类配置如下,此处一开始与配置文件关联的prefix为驼峰命名(foodProperties),引发上面的错误;
@Component
@ConfigurationProperties(prefix = "configuration.foodProperties")
@Data
public class FoodProperties {
private String name ;
}
配置文件application.yml:
configuration:
foodProperties:
name: MilkTea
@ConfigurationProperties支持驼峰命名和“-”连接命名生成bean文件,但与配置文件关联的prefix不支持
将Component类和配置文件中的foodProperties改为foodproperties,即可解决该错误。
附:SpringBoot之@ConfigurationProperties与@Value的区别
3 application.yml 和application.properties 优先级
application.yml 文件会被优先加载,
而如果同时存在 application.properties 文件,并且存在相同的配置,
那么则会用 application.properties 文件中的配置覆盖之前的配置;
也就是说哪个文件被最后加载,哪个才具有最高级别,
因为最后的,会覆盖前面所有的。
4 Springboot 禁用数据库自动配置
在springboot项目,如果你在pom.xml里面加入 mybatis-spring-boot-starter 包,而在application.yml配置文件没有配置数据库连接信息,启动则会报错,如下:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
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).
解决方法
在Application类上增加:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@Configuration
@ComponentScan(basePackages = {"com.example.demo"})
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableCaching
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5 DBEAVER连接MySQL运行报错(时区错误)
The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than
time zone 时区错误
DBEAVER连接MySQL运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
使用root用户登录mysql,按照如下图所示操作即可。
show variables like '%time_zone%';
SYSTEM为SQL默认美国时间,而我们中国要比他们迟8小时
插播个去 图片水印 的问题解决(转载)因此将时区设置为当前系统时区即可,
因此,采用+8:00格式
set global time_zone='+8:00';
6 mybatis插入数据失败
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'zhangsan' for key 'UK_lqjrcobrh9jc8wpcar64q1bfh'
由于索引的存在,插入数据的字段内容重复导致插入失败
7 tk.mybatis
已经在启动类配置了dao扫描路径还是提示dao自动加载失败
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@MapperScan(basePackages = "com.example.hello.spring.boot.mybatis.dao")//dao路径
public class HelloSpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootMybatisApplication.class, args);
}
}
解决方法:
给dao加上注释
8 zuul 使用serviceId 聚合服务失败
https://www.cnblogs.com/lexiaofei/p/7098702.html