我有一个简单的HTTP GET请求,如下所示:
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());
}
但是@RequestParam
“描述”的值是“1 3”。
我做错了什么?
或者,我应该做些什么,以便在Spring @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>
2条答案
按热度按时间9cbw7uwe1#
所有内容都符合预期。根据URL编码中的RFC3986,
+
是保留字符。根据第2.2点,保留字符:如果在URI组件中找到保留字符,并且该字符没有已知的定界角色,则必须将其解释为表示与该字符的US-ASCII编码对应的数据八位字节。
要使用
+
作为值,您需要将其编码为%2B
,如百分比编码保留字符中所述。这将使您的URL:您可以通过以下方式访问本机:
请注意,有时Spring在处理
+
时不一致,例如SPR-16860 Spring is inconsistent in the encoding/decoding of URLs bug。bn31dyow2#
缺少与反序列化无关的URL Encoding,
+
由%2B编码当你发送HTTP请求时,你应该对你的参数值进行编码,在你的例子中,
+
应该替换为%2B
:对于Angular 使用HttpUrlEncodingCodec
encodeValue(value: string)