How to resolve Chinese character encoding issues in Spring Boot?

In Spring Boot, you can solve the issue of receiving garbled Chinese characters by configuring the character encoding.

First, add the following configuration in the application.properties file.

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

This will enable character encoding and set the character set to UTF-8.

Then, in the Controller, you can use the @RequestParam annotation to receive Chinese parameters.

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

When sending a request from the front end, make sure to set the Content-Type to application/x-www-form-urlencoded;charset=UTF-8.

After this configuration, Spring Boot will be able to handle Chinese parameters correctly and avoid garbled characters issues.

bannerAds