Spring MVC @RequestParam encoding problem on UTF-8
See original GitHub issueI develop a rest API that serves city information for users. But I’m having problems with @RequestParam charset. I sent some params to the controller which includes Turkish characters. And it didn’t work as expected.
Some of the characters that I have Turkish character problem:
- ı
- ö
- ş
- ç
If you want to check that source code of this project: https://github.com/enesusta/tzone/tree/develop
Here is my request
http://localhost:8080/district?name=diyarbakı
Here is our controller
import com.github.enesusta.tzone.district.service.DistrictsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
@RestController
public class DistrictsController {
@Autowired
DistrictsService districtsService;
@Autowired
Executor executor;
@GetMapping("/districts")
public CompletionStage<List<Districts>> getDistricts() {
return CompletableFuture.supplyAsync(districtsService.getDistricts(), executor);
}
@GetMapping(value = "/district", produces = "application/json;charset=utf-8")
public CompletionStage<List<Districts>> getDistrictsByName(@RequestParam(name = "name") String districtName) throws UnsupportedEncodingException {
return CompletableFuture.supplyAsync(districtsService.findDistrictsByCityName(districtName), executor);
}
}
Here is our service:
import com.github.enesusta.tzone.district.Districts;
import com.github.enesusta.tzone.district.repository.DistrictsRepository;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.function.Supplier;
@Service
public class DefaultDistrictsService implements DistrictsService {
private final static Logger logger = Logger.getLogger(DefaultDistrictsService.class);
@Autowired
DistrictsRepository districtsRepository;
@Autowired
Locale locale;
@Override
public Supplier<List<Districts>> getDistricts() {
return () -> districtsRepository.getDistricts();
}
@Override
public Supplier<List<Districts>> findDistrictsByCityName(String cityName) throws UnsupportedEncodingException {
final String encodedParam = URLEncoder.encode(cityName, StandardCharsets.UTF_8);
logger.info("encodedParam =" + encodedParam);
final String upperCasedFirstCharacter = encodedParam.substring(0, 1).toUpperCase(locale);
final String query = String.format("%s%s", upperCasedFirstCharacter, encodedParam.substring(1));
logger.info("query = " + query);
return () -> districtsRepository.findDistrictsByCityName(query);
}
}
Expected behavior
name=diyarbakı
encodedParam = diyarbakı
query = Diyarbakı
Actual behavior
name = diyarbak%25C4%25B1
encodedParam = diyarbak%25C4%25B1
query = Diyarbak%25C4%25B1
Environment
- uname -a
Linux DESKTOP-B3CNSC5 4.4.0-18362-Microsoft #836-Microsoft Mon May 05 16:04:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
- java -version
openjdk 11.0.6 2020-01-14
OpenJDK Runtime Environment GraalVM CE 20.0.0 (build 11.0.6+9-jvmci-20.0-b02)
OpenJDK 64-Bit Server VM GraalVM CE 20.0.0 (build 11.0.6+9-jvmci-20.0-b02, mixed mode, sharing)
- mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /mnt/c/Java/apache-maven-3.6.3
Java version: 11.0.6, vendor: Oracle Corporation, runtime: /home/phield/java/graalvm-ce-java11-20.0.0
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-18362-microsoft", arch: "amd64", family: "unix"
- quarkus-ver
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>1.5.2.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.5.2.Final</quarkus.platform.version>
<surefire-plugin.version>2.22.1</surefire-plugin.version>
<jacoco.version>0.8.4</jacoco.version>
</properties>
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Spring MVC UTF-8 Encoding - java - Stack Overflow
Easiest solution to force UTF-8 encoding in Spring MVC returning String: In @RequestMapping , use: produces = MediaType.APPLICATION_JSON_VALUE + " ...
Read more >Java – UTF-8 encoding problem in Spring MVC - iTecNote
I' ve a Spring MVC bean and I would like to return turkish character by setting encoding UTF-8. but although my string is...
Read more >[Solved]-Spring MVC UTF-8 Encoding-Spring MVC
Easiest solution to force UTF-8 encoding in Spring MVC returning String: In @RequestMapping , use: produces = MediaType.APPLICATION_JSON_VALUE + " ...
Read more >UTF-8 encoding a JSP with Spring MVC
So how does Spring MVC set the response encoding of a JSP to UTF-8? It's a trick question. The short answer is that...
Read more >Guide to Java URL Encoding/Decoding - Baeldung
When encoding URI, one of the common pitfalls is encoding the complete URI. Typically, we need to encode only the query portion of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think we’ll be backporting the RESTEasy bump to the 1.8.x line. In any case, 1.9.x isn’t far off
@gsmet I’m not very familiar with the constraints around this in Quarkus. Is there any chance of this fix landing as part of 1.8.2? The bump to 4.5.8 also fixes https://issues.redhat.com/browse/RESTEASY-2678 which I am also encountering.