一、小白坑

1 页面404

SpringBoot 踩坑集锦 - 图1image.gif
1. 确认输入项目路径是否正确,如:http://localhost:8080/index,这里需要注意的是端口号的查看
image.gif
2. 确认注解是否用对

  1. Controller 层类上面使用的注解是 @RestController 而并非是 @Controller,或者是 @Controller + @ResponseBody
  2. 详解:如果返回 String 或者 json 的话就直接类上用 @RestController
  3.    如果想要页面跳转的话,就使用 @Controller
  4.    如果只有在某方法上返回 json,其他方法跳转页面,则在类上添加 @Controller,在需要返回 String json 的方法上添加 @ResponseBody 注解;

image.gif
3. 确认导入项目注解的包地址是否正确

  1. @RequestMapping("/index")
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. @RestController
  4. import org.springframework.web.bind.annotation.RestController;

image.gif
4. 启动类位置错误
spring-boot会自动加载启动类所在包下及其子包下的所有组件,且启动类不能直接放在main下,要放在包里

2 @ConfigurationProperties注解松散绑定错误

项目启动时报错,bean初始化失败

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Configuration property name 'foodProperties' is not valid:
  6. Invalid characters: 'P'
  7. Bean: foodProperties
  8. Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter
  9. Action:
  10. Modify 'foodProperties' so that it conforms to the canonical names requirements.
  11. Process finished with exit code 1

image.gif
Component 类配置如下,此处一开始与配置文件关联的prefix为驼峰命名(foodProperties),引发上面的错误;

  1. @Component
  2. @ConfigurationProperties(prefix = "configuration.foodProperties")
  3. @Data
  4. public class FoodProperties {
  5. private String name ;
  6. }

image.gif
配置文件application.yml:

  1. configuration:
  2. foodProperties:
  3. name: MilkTea

image.gif
@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配置文件没有配置数据库连接信息,启动则会报错,如下:

  1. Description:
  2. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  3. Reason: Failed to determine a suitable driver class
  4. Action:
  5. Consider the following:
  6. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  7. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

image.gif
解决方法
在Application类上增加:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

  1. @Configuration
  2. @ComponentScan(basePackages = {"com.example.demo"})
  3. @SpringBootApplication
  4. @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
  5. @EnableCaching
  6. public class DemoApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(DemoApplication.class, args);
  9. }
  10. }

image.gif

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自动加载失败

  1. @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
  2. @MapperScan(basePackages = "com.example.hello.spring.boot.mybatis.dao")//dao路径
  3. public class HelloSpringBootMybatisApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(HelloSpringBootMybatisApplication.class, args);
  6. }
  7. }

image.gif

  1. 解决方法:

image.gif
给dao加上注释
SpringBoot 踩坑集锦 - 图13image.gif

8 zuul 使用serviceId 聚合服务失败

https://www.cnblogs.com/lexiaofei/p/7098702.html