溫馨提示×

springboot接收中文亂碼怎么解決

小億
501
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注解來接收中文參數(shù):

@GetMapping("/example")
public String example(@RequestParam("name") String name) {
    // 處理中文參數(shù)
    return "Hello " + name;
}

在前端發(fā)送請求時,確保請求的Content-Type設置為application/x-www-form-urlencoded;charset=UTF-8。

這樣配置后,Spring Boot將能夠正確處理中文參數(shù)并避免亂碼問題。

0