Spring请求参数中+(加号)的反序列化

ntjbwcob  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(174)

我有一个简单的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>
9cbw7uwe

9cbw7uwe1#

所有内容都符合预期。根据URL编码中的RFC3986+是保留字符。根据第2.2点,保留字符:
如果在URI组件中找到保留字符,并且该字符没有已知的定界角色,则必须将其解释为表示与该字符的US-ASCII编码对应的数据八位字节。
要使用+作为值,您需要将其编码为%2B,如百分比编码保留字符中所述。这将使您的URL:
您可以通过以下方式访问本机:
请注意,有时Spring在处理+时不一致,例如SPR-16860 Spring is inconsistent in the encoding/decoding of URLs bug。

bn31dyow

bn31dyow2#

缺少与反序列化无关的URL Encoding
+由%2B编码
当你发送HTTP请求时,你应该对你的参数值进行编码,在你的例子中,+应该替换为%2B

http://localhost:8080/search?page=0&size=20&sort=id,asc&description=1%2B3

对于Angular 使用HttpUrlEncodingCodecencodeValue(value: string)

相关问题