gpt4 book ai didi

java - Spring Requestparam中+(plus)的反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:58 26 4
gpt4 key购买 nike

我有一个像这样的简单 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 "description"的值是 "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>

最佳答案

一切如期而至。根据 RFC3986在 URL 编码中 + 是一个保留字符。根据第 2.2 点。保留字符:

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.

要使用 + 作为值,您需要将其编码为 %2B,如 Percent-encoding reserved characters 中所述。 .这将使您的网址:

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错误。

关于java - Spring Requestparam中+(plus)的反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com