一尘不染

在Spring Request参数中对+(加号)进行反序列化

spring-boot

我有一个简单的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”。

我究竟做错了什么?

或者我应该怎么做才能在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>

阅读 291

收藏
2020-05-30

共1个答案

一尘不染

一切都如预期。按照RFC3986中的URL编码,它+是一个保留字符,已解码为空格。要+用作值,您需要%2B按照百分比编码保留字符中的说明对其进行编码。这将使您的URL:

[http:// localhost:8080 / search?page = 0&size = 20&sort = id,asc&description

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

请注意,有时在+处理时Spring会不一致,例如SPR-16860
Spring在URL编码/解码
错误中会不一致

2020-05-30