Spring Controller 用空格替换参数中的 + 字符

问题描述

我有一个带 2 个参数的控制器 -

@PostMapping(value = "/myEndPoint")
public ResponseEntity<streamingresponsebody> process(final InputStream data,@RequestParam final String param1,@RequestParam final String base64Param)  {
    return ok().body(doSomething(base64Param.getBytes(),param1,data));
}

base64Param String 参数将包含 + 字符,但 Spring 控制器当然会用空格替换这些字符。

你知道我如何解决这个问题并告诉控制器不要理会 + 字符吗?

更新- 我试过了

UriComponentsBuilder builder = fromUriString(format("http://localhost:%s/%s",serverPort,"/myEndPoint"))
            .queryParam("param1",param1)
            .queryParam("base64Param",Base64.getUrlEncoder().encodetoString(base64ParamValueWithPluses.getBytes()));

但我必须像这样解码控制器中的 base64Param 参数 -

String decodedBaseParam64 = Base64.getUrlDecoder().decode(encodeBaseParam64);

有没有办法对此进行编码,以便 Spring 自动解码并保留 + 字符?

解决方法

URI uri = fromUriString(format("http://localhost:%s/%s",serverPort,"/myEndPoint"))
            .queryParam("param1",param1)
            .queryParam("base64Param","{base64Param}")
            .build(base64Param);

来源 - https://github.com/spring-projects/spring-framework/issues/21577