我正在尝试使用Spring的ServletUriComponentsBuilder
来创建当前请求的分页next
和prev
链接。
我遇到的问题是ServletUriComponentsBuilder.fromCurrentRequest()
并没有解码百分比编码的值,如:
http://example.com/articles?page%5Bnumber%5D=2
问题是使用可能使用未编码的方括号调用页面,如http://example.com/articles?page[number]=2
,没有任何问题。
Spring Data在它的可分页参数解析器中接受这两种变体(包括未编码的方括号和编码的方括号)。 这就是在水下使用Coyote web request get参数,其中包含未编码的参数名称。
Spring @RequestParam("page[number]")
也接受了http://example.com/articles?page%5Bnumber%5D=2
等编码请求,没有任何问题。
从服务器端,我总是希望根据RFC 3986返回百分比编码的URL。
但似乎没有办法解决这个问题,因为UriComponents
查询参数可能包含编码的未编码名称。因为,如果我在构建器上调用encode()
,已经编码的查询参数会被另一次编码,但是如果包含未编码的名称toURI()
将失败,因为未编码的[
是不允许的
请注意,除了分页之外,url可能包含多个查询参数,例如用于过滤。
请求可以像:
http://example.com/articles?filter[category]=food
并会返回带有编码next
链接的回复,例如:
http://example.com/articles?page%5Bnumber%5D=2&filter%5Bcategory%5D=food
我的解决方法是忽略ServletUriComponentsBuilder
并简单地获取请求网址并执行自定义regexp替换。
答案 0 :(得分:0)
我知道这是一个较旧的问题,您还找到了一种解决方法。但是您是否尝试使用ServletUriComponentsBuilder
的{{1}}方法?
以下几种类型:
build()
在处理JSON字符串时遇到一些问题,这很有帮助。