温馨提示×

springboot接收中文乱码怎么解决

小亿
201
2024-01-11 03:15:51
栏目: 编程语言

在Spring Boot中,可以通过配置字符编码来解决接收中文乱码的问题。

首先,在application.properties文件中添加以下配置:

spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8

这将启用字符编码,并将字符集设置为UTF-8。

然后,在Controller中,可以使用@RequestParam注解来接收中文参数:

@GetMapping("/example")
public String example(@RequestParam("name") String name) {
    // 处理中文参数
    return "Hello " + name;
}

在前端发送请求时,确保请求的Content-Type设置为application/x-www-form-urlencoded;charset=UTF-8。

这样配置后,Spring Boot将能够正确处理中文参数并避免乱码问题。

0