Spring Requestparam 中 +(加号)的反序列化

2024-04-05

我有一个简单的 HTTP GET 请求,如下所示:

还有一个 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”。

我究竟做错了什么?

或者我应该做什么,以便像“+”这样的信号在 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 https://www.rfc-editor.org/rfc/rfc3986在 URL 编码中+是保留字符。按照第 2.2 点。保留字符:

如果在 URI 组件中找到保留字符,并且该字符没有已知的定界角色,则必须将其解释为表示与 US-ASCII 中该字符的编码相对应的数据八位字节。

To use +作为一个值,您需要将其编码为%2B如中所解释的保留字符的百分比编码 https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters。这将使您的网址:

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

请注意,有时 Spring 在涉及到时会不一致+处理例如SPR-16860 Spring 在 URL 的编码/解码方面不一致 https://jira.spring.io/browse/SPR-16860 bug.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Requestparam 中 +(加号)的反序列化 的相关文章

随机推荐