朋也的博客 » 首页 » 文章
作者:朋也
日期:2024-08-15
类别:杂项
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
刚新建的springboot web项目,写了一个接口,从数据库中查询数据输出json时,浏览器上看到的是乱码
@Controller
class HomeController(@Autowired private val jdbcClient: JdbcClient) {
@GetMapping("/")
@ResponseBody
fun index(): Any? {
return jdbcClient.sql("select * from user limit 20").query(User::class.java).list()
}
}
数据库编码是UTF-8
,且中文显示正常
页面上效果
出现这问题的原因是springboot web默认输出不会在 application/json
后添加上 charset=utf-8
了
两种解决办法
response.characterEncoding = "utf-8"
@GetMapping("/")
@ResponseBody
fun index(response: HttpServletResponse): Any? {
response.characterEncoding = "utf-8"
return jdbcClient.sql("select * from user limit 20").query(User::class.java).list()
}
打开 application.properties
添加上 server.servlet.encoding.force-response = true
配置
然后再访问就能正常显示了