我有一个简单的HTTP GET请求,如下所示:
http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1+3
还有一个RestController:
@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());
}
但是@RequestParam
“描述”的值变为“ 1 3”。
我在做什么错了?
或者我应该怎么做才能在春季@RequestParam
中将类似“ +”的信号反序列化为“ +”?
这是我的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>
答案 0 :(得分:1)
您缺少与反序列化无关的URL Encoding,
+
由%2B编码
发送HTTP请求时,应对参数值进行编码,在这种情况下,+
应替换为%2B
:
http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1%2B3
对于角度使用HttpUrlEncodingCodec encodeValue(value: string)
答案 1 :(得分:1)
一切都如预期。按照URL编码+
中的RFC3986,是保留字符,解码为空格。要将+
用作值,您需要按照Percent-encoding reserved characters中的说明将其编码为%2B
。这将使您的URL:
http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1%2B3
请注意,在处理+
时,有时Spring会不一致。 SPR-16860 Spring is inconsistent in the encoding/decoding of URLs错误。