Spring ServletUriComponentsBuilder和查询参数中的方括号

问题描述 投票:2回答:1

我正在尝试使用Spring的ServletUriComponentsBuilder从当前请求中创建分页nextprev链接。

我遇到的问题是,ServletUriComponentsBuilder.fromCurrentRequest()并未对百分比编码的值进行未编码,例如:

http://example.com/articles?page%5Bnumber%5D=2

问题是用途可以调用了带有未编码方括号的页面,例如http://example.com/articles?page[number]=2,没有任何问题。

Spring Data在其可分页参数解析器中接受两种变体(未编码的方括号和已编码的方括号)。事实是,在水下使用了Coyote Web请求get参数,其中包含未编码的参数名称。

而且Spring的@RequestParam("page[number]")毫无问题地接受http://example.com/articles?page%5Bnumber%5D=2之类的编码请求。

从服务器端,我总是想根据RFC 3986返回百分比编码的url。

但是似乎没有办法,因为UriComponents查询参数可能包含未编码的编码名称。因此,如果我在构建器上调用encode(),则已经编码的查询参数将再次进行编码,但是如果包含未编码的名称,则toURI()将失败,因为不允许使用未编码的[

请注意,除了分页外,该网址可能还包含多个查询参数,例如用于过滤。

一个请求可能会出现:

http://example.com/articles?filter[category]=food

并且将返回带有编码的next链接的响应,例如:

http://example.com/articles?page%5Bnumber%5D=2&filter%5Bcategory%5D=food

我的解决方法是忽略ServletUriComponentsBuilder并仅获取请求url并执行自定义正则表达式替换。

spring spring-mvc spring-data spring-hateoas
1个回答
0
投票

我知道这是一个较旧的问题,您还找到了一种解决方法。但是您是否尝试使用ServletUriComponentsBuilderbuild()方法?以下几种:

ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString();

我在处理JSON字符串时遇到了一些问题,这有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.