Issue
I have a simple HTTP GET request like this:
http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1+3
@RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<TestEntity>> search(Pageable pageable, @RequestParam("description") String description) {
Page<TestEntity> page = service.search(pageable, description);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page);
return ResponseEntity
.ok()
.headers(headers)
.body(page.getContent());
}
But the value of @RequestParam
"description" is getting "1 3".
What am I doing wrong?
Or what should I do so that signals like "+" are deserialized to "+" in the Spring @RequestParam
?
This is my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<project.http.version>1.23.0</project.http.version>
<project.oauth.version>1.23.0</project.oauth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Solution
Everything is as expected. As per RFC3986 in URL encoding +
is a reserved character. As per point 2.2. Reserved Characters:
If a reserved character is found in a URI component and no delimiting role is known for that character, then it must be interpreted as representing the data octet corresponding to that character's encoding in US-ASCII.
To use +
as a value you need to encode it as %2B
as explained in Percent-encoding reserved characters. This will make your URL:
http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1%2B3
Do note that sometimes Spring is inconsistent when it comes to +
handling e.g. SPR-16860 Spring is inconsistent in the encoding/decoding of URLs bug.
Answered By - Karol Dowbecki
Answer Checked By - Mildred Charles (JavaFixing Admin)