学习Springboot遇到的一些坑

IDEA 上中文乱码的问题,解决方案记录

1.读取properties文件中的中文乱码

通过如下的方法 注:勾上之后,最好把文件先备份后删掉,再新建一次。建议把IDEA的编码格式都配置成UTF-8
  IDEA自带的

2.读取数据库中的中文乱码
检查数据库的字符集编码格式,改成utf8
检查数据连接的url加入characterEncoding=urf8
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8

3.返回到前端的数据乱码
在配置文件,建议将.properties改成.yml 这样比较清晰,加入下面的代码,这是*.yml的写法。

spring:
  http:
    encoding:
      force: true

这是*.properties的写法

spring.http.encoding.force=true

写在controller文件夹里面的url访问不到

方法1:修改文件结构
将启动的SpringBootApplication.java文件controller文件夹放到同一目录
方法2:在SpringBootApplication.java文件中,添加注解 @ComponentScan(“包名”)

调用save()方法报错:ids for this class must be manually assigned before calling save():

org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save(): com.boe.tww.entity.LoggerEntity; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.boe.tww.entity.LoggerEntity

在实体类中加入
@GeneratedValue(strategy=GenerationType.AUTO)
解决save方法报错的问题

其他的细节优化方面

@RequestMapping(path=”/user”,method=RequestMethod.GET)
可以写成GetMapping(“/user”)

@RequestMapping(path=”/user”,method=RequestMethod.POST)
可以写成PostMapping(“/user”)

附上学习的博主,非常感谢
Springboot学习

猜你喜欢

转载自blog.csdn.net/u013788943/article/details/79550153